in reply to accessing array elements using a reference

It seems that Perl figures that if you know enough to use references then you must know enough to pick the sigil you wanted. The warning doesn't seem to be triggered any time there's a reference involved.

I suppose you might reasonably call this a bug. Want to try to fix it? Start by reading perlhack!

-sam

  • Comment on Re: accessing array elements using a reference

Replies are listed 'Best First'.
Re^2: accessing array elements using a reference
by Anonymous Monk on May 19, 2008 at 04:06 UTC
    Thanks for your reply. I would like to note that even when a warning is generated when i use @array[0], the output is the same as when i use what perl suggests i.e. $array[0].
    So both ways @array[0] vs $array[0] produce the same result but the former generates a warning.

      @array[0] and $array[0] are very similar. In fact, in scalar context, they evaluate to the same thing. warnings aside, there are two differences between slices and indexed elements. First, the index expression for the slice is evaluated in list context while the index expression for the element is evaluated in scalar context. Second, the slice creates a list context when used as an lvalue while the element creates a scalar context.

        Thanks for the tip. But I still don't understand why
        print @{@{$hash{"one"}}[0]}, "\n";
        does not generate a warning instructing me to use
        print @{${$hash{"one"}}[0]}, "\n";
        while
        my @array = (4,5,6); print @array[0];
        generates a warning instructing me to use $array[0].
        Anyone? Thanks a lot in advance