Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

using range gives warning

0 views
Skip to first unread message

Paul Lalli

unread,
Oct 22, 2004, 1:29:03 PM10/22/04
to
I seem to have found an odd ... well, I don't want to say "bug", because
that would be arrogant of me. Nonetheless, I am thoroughly confused.
Perhaps someone can help explain:

use strict;
use warnings;
my @foo;
$foo[0] = (6..10);
print "Foo: @foo\n";
__END__

This small program returns a warning:
Use of uninitialized value in range (or flip) at - line 4.

And output of:
Foo:

If I switch line four to:
$foo[0] = (6,7,8,9,10);
I get the output I expect (four warnings about using a scalar in void
context, followed by assigning $foo[0] to 10).

This gets more bizzare if I add
use diagnostics;
after use warnings. Rather than pulling the correct warning explanation
from perldiag, the warning disappears altogether.

I have verified this on Perl 5.8.0 on solaris, and 5.8.3 (ActiveState)
on Win2k.

Can someone explain to me what's going on? Why would using a range of
6..10 produce a warning like that, when the expansion does not?

Oh, and just to explain the code - it was written to help explain to
someone a situation in which using @foo[0] instead of $foo[0] would
produce incorrect results, rather than simply a warning message.

Thank you,
Paul Lalli

--

--
Paul Lalli
Senior Software Engineer
ES11 Information Systems
8 Stanley Circle, Suite #8
Latham, NY 12110
p 518-782-1111
f 518-782-1212
e p...@es11.com


Paul Lalli

unread,
Oct 22, 2004, 1:38:17 PM10/22/04
to
"Paul Lalli" <mri...@gmail.com> wrote in message
news:zrbed.35$9R4.19@trndny09...

> I seem to have found an odd ... well, I don't want to say "bug",
because
> that would be arrogant of me. Nonetheless, I am thoroughly confused.
> Perhaps someone can help explain:
>
> use strict;
> use warnings;
> my @foo;
> $foo[0] = (6..10);
> print "Foo: @foo\n";
> __END__
>

I should have tried this first, of course, so my apologies. But this
odditty is demonstrated in simpler terms without the array messiness:

use strict;
use warnings;
my $foo;
$foo = (6..10);
print "Foo: $foo\n";
__END__

(The rest of my post still applies to this new code, so I'm leaving it
intact below)

Paul Lalli

Paul Lalli

unread,
Oct 22, 2004, 1:41:59 PM10/22/04
to
"Paul Lalli" <mri...@gmail.com> wrote in message
news:zrbed.35$9R4.19@trndny09...
> I seem to have found an odd ... well, I don't want to say "bug",
because
> that would be arrogant of me. Nonetheless, I am thoroughly confused.
> Perhaps someone can help explain:
>
> use strict;
> use warnings;
> my @foo;
> $foo[0] = (6..10);
> print "Foo: @foo\n";
> __END__
>
> This small program returns a warning:
> Use of uninitialized value in range (or flip) at - line 4.

And now I realize my error - I wasn't using .. to create a list, I was
using the scalar "flip" operator without realizing it, because I was
assigning to a scalar. And the "uninitialized value" refers to the $.
variable, because I'm not reading a file.

Apologies for wasting bandwidth. Really wish I could retract this whole
thread...

Paul Lalli

Uri Guttman

unread,
Oct 22, 2004, 1:42:27 PM10/22/04
to
>>>>> "PL" == Paul Lalli <mri...@gmail.com> writes:

PL> use strict;
PL> use warnings;
PL> my $foo;
PL> $foo = (6..10);

range is very different in list and scalar contexts. rtfm. :)

uri

Brian McCauley

unread,
Oct 22, 2004, 1:42:26 PM10/22/04
to

Paul Lalli wrote:

> I seem to have found an odd ... well, I don't want to say "bug", because
> that would be arrogant of me. Nonetheless, I am thoroughly confused.
> Perhaps someone can help explain:
>
> use strict;
> use warnings;
> my @foo;
> $foo[0] = (6..10);

> This small program returns a warning:


> Use of uninitialized value in range (or flip) at - line 4.

You appear to be confused by the behaviour of the range operator.

Have you considered the possibility of reading the explaination of the
behaviour of the range operator in the reference manual? (At least as
far as the end of the first sentence).

Looking up the thing you are confused about in the reference manual
should come earlier in you problem solving path than posting to Usenet.

Range Operators

Binary ".." is the range operator, which is really two
different operators depending on the context. In list
context, it [ is a range operator and does what you expect]

In scalar context, ".." [it is a "flip" operator and does
something else entirely].

If you read the description of the something else it will become clear
that it is the special variable $. that is undefined.

Paul Lalli

unread,
Oct 22, 2004, 1:48:55 PM10/22/04
to
"Brian McCauley" <nob...@mail.com> wrote in message
news:clbghd$hqd$1...@sun3.bham.ac.uk...

> You appear to be confused by the behaviour of the range operator.
>
> Have you considered the possibility of reading the explaination of the
> behaviour of the range operator in the reference manual? (At least as
> far as the end of the first sentence).
>
> Looking up the thing you are confused about in the reference manual
> should come earlier in you problem solving path than posting to
Usenet.

Yup. And in fact, I did read perldoc perlop prior to posting - but
because of the circumstance in which I found this error, I was looking
for an explanation regarding something that had to do with assigning to
an array element. It simply didn't occur to me that I was using scalar
context.

My apologies again

Paul Lalli


Scott W Gifford

unread,
Oct 22, 2004, 2:05:14 PM10/22/04
to
"Paul Lalli" <mri...@gmail.com> writes:

> "Paul Lalli" <mri...@gmail.com> wrote in message

[...]

>> Perhaps someone can help explain:
>>
>> use strict;
>> use warnings;
>> my @foo;
>> $foo[0] = (6..10);
>> print "Foo: @foo\n";
>> __END__

[...]

>> This small program returns a warning:
>> Use of uninitialized value in range (or flip) at - line 4.

I don't know exactly what's going on, but perhaps the .. is being
treated as a flip-flop operator, which is different from what you
expect.

perlop(1) says:

In scalar context, ".." returns a boolean value. The operator is
bistable, like a flip-flop, and emulates the line-range (comma)
operator of sed, awk, and various editors.

and later:

If either operand of scalar ".." is a constant expression, that
operand is considered true if it is equal ("==") to the current
input line number (the $. variable).

So I suspect that it's checking if $. == 6, but you haven't read any
files, so $. is undef, causing that error.

----ScottG.

A. Sinan Unur

unread,
Oct 22, 2004, 3:08:25 PM10/22/04
to
"Paul Lalli" <mri...@gmail.com> wrote in news:HDbed.110$Xq3.65@trndny01:

> Apologies for wasting bandwidth. Really wish I could retract this whole
> thread...

Actually, thank you. I actually learned something and learned it in a way
that (I hope) makes it hard to forget.

Sinan.

David K. Wall

unread,
Oct 22, 2004, 4:44:21 PM10/22/04
to
Paul Lalli <mri...@gmail.com> wrote:

This simply won't do, Paul. Haven't you learned anything here? You're
supposed to flame Brian for being rude to you and demand that he
parrot the documentation to you rather than reading it for yourself.
Then when others follow up that post, you call them all losers and
either leave in a huff or stick around for days making an ass of
yourself. Top-posting with full-quote is optional, but highly
recommended for the full effect. Or you could have not quoted any
text at all. A simple heartfelt "Fuck you" goes a long way toward
making a lasting impression.

But instead you were polite and agreeable. You even admitted you had
made a mistake, and apologized on top of that! Sheesh! What kind of
usenet poster are you?

Brian McCauley

unread,
Oct 23, 2004, 3:47:41 AM10/23/04
to

David K. Wall wrote:

> Paul Lalli <mri...@gmail.com> wrote:
>
>
>>"Brian McCauley" <nob...@mail.com> wrote in message
>>news:clbghd$hqd$1...@sun3.bham.ac.uk...
>>
>>>You appear to be confused by the behaviour of the range operator.
>>>
>>>Have you considered the possibility of reading the explaination
>>>of the behaviour of the range operator in the reference manual?
>>>(At least as far as the end of the first sentence).
>>>
>>>Looking up the thing you are confused about in the reference
>>>manual should come earlier in you problem solving path than
>>>posting to Usenet.
>>
>>Yup. And in fact, I did read perldoc perlop prior to posting -
>>but because of the circumstance in which I found this error, I was
>>looking for an explanation regarding something that had to do with
>>assigning to an array element. It simply didn't occur to me that
>>I was using scalar context.
>>
>>My apologies again
>
>
> This simply won't do, Paul. Haven't you learned anything here? You're
> supposed to flame Brian for being rude to you and demand that he
> parrot the documentation to you rather than reading it for yourself.

And now I am going to mess things up further by apologising that my
response was not clearly enough tounge-in-cheek - it really could have
done with a few smilies. I didn't really think that Paul hadn't RTFM.

Uri Guttman

unread,
Oct 23, 2004, 9:36:44 AM10/23/04
to
>>>>> "BM" == Brian McCauley <nob...@mail.com> writes:

BM> David K. Wall wrote:

>> This simply won't do, Paul. Haven't you learned anything here?
>> You're supposed to flame Brian for being rude to you and demand that
>> he parrot the documentation to you rather than reading it for
>> yourself.

BM> And now I am going to mess things up further by apologising that
BM> my response was not clearly enough tounge-in-cheek - it really
BM> could have done with a few smilies. I didn't really think that
BM> Paul hadn't RTFM.

oh, he did rtfm. the maroon didn't read his own code properly! (BIG :)
and i agree with everything david said about him too!

uri

--
Uri Guttman ------ u...@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org

0 new messages