morgon has asked for the wisdom of the Perl Monks concerning the following question:
I am a bit surprised by this:
I understand the first bit. I assign an array to a scalar, so the array is evaluated in scalar context, giving it's size, so $x is 2.my @a = ("a", "b"); my $x = @a; my $y = ("a", "b"); print "$x $y\n"; # prints "2 b"
But the second bit is surprising to me...
I would have expected $y also to be 2, but instead the last element of the list is used instead (add more elements and you'll see it's always the last).
Is that intentional or documented somewhere (I am running 5.16.2)?
It certainly violates the principle of least surprise for me but maybe there is a good reason for this behaviour...
Can someone enlighten me?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: arrays and lists, hmmm
by Athanasius (Archbishop) on Apr 26, 2016 at 03:29 UTC | |
by morgon (Priest) on Apr 27, 2016 at 02:02 UTC | |
|
Re: arrays and lists, hmmm
by beech (Parson) on Apr 26, 2016 at 02:34 UTC | |
|
Re: arrays and lists, hmmm
by Marshall (Canon) on Apr 26, 2016 at 03:58 UTC | |
|
Re: arrays and lists, hmmm
by haukex (Archbishop) on Apr 26, 2016 at 09:04 UTC | |
|
Re: arrays and lists, hmmm
by ikegami (Patriarch) on Apr 26, 2016 at 15:20 UTC |