>There are no array slices in your examples. In a [] subscript,
>expressions are evaluated in scalar context.

Well, not in the case of

    @a[0, 1, 2]

It is tempting for the programmer to try the same thing with a listref:

    $r->[0, 1, 2]

although in fact the correct way to do it is

    @$r[0, 1, 2]

A warning for the second case would be a good idea, since it's a little
counterintuitive that the square brackets behave differently in the two
cases.  (The programmer should remember that the character at the front
of the expression gives the context, so $r->[whatever] will always be
a scalar and @$r[whatever] always a list.)

>># Strangeness with .. operator
>>print Dumper map { [ $_->[0 .. 1] ] } @data;
>
>That's a flip-flop, not a range, due to scalar context.
>It's consistent with this :
>
>    $ perl -le 'print $a while $a = 0..1' | head
>    1
>    2
>    3
>    4
>    5
>    6
>    7
>    8
>    9
>    10

Oh, how strange.  I was only dimly aware of this flip-flop operator.
Thanks for clarifying.

>but I don't explain this behaviour, since 0 should be false.
>Might be a bug.

perlop says:

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

So somehow it must be comparing 0 and 1 to $., which in this example
program is always undef since we haven't read a line from input.

I'll remember to be careful when using .. for ranges in case I get the
wrong operator by accident.

-- 
Ed Avis <[EMAIL PROTECTED]>

Reply via email to