MeowChow has asked for the wisdom of the Perl Monks concerning the following question:
So far so good... now substitute $#_ + 1 for scalar @_:sub li { shift; @_ ? (scalar @_, &li) : (); } print li qw(a b c d e f g); # prints 654321
Que bueno! Moving on, lets remove the "+1" ...sub li { shift; @_ ? ($#_ + 1, &li) : (); } print li qw(a b c d e f g); # prints 654321
What is this? Apparently, $#_ is now evaluated after the recursive call. To my eyes, this is a rather unexpected result, especially when you consider how the comma operator behaves in a scalar context, and my first two examples. This phenomenon affects globals as well:sub li { shift; @_ ? ($#_, &li) : (); } print li qw(a b c d e f g); # prints -1-1-1-1-1-1
Update: remarkably, I've just determined that this behaviour is tied to the way in which the subroutine call is performed: if the calls above are switched from &li to li(@_), the order of evaluation goes back to normal. How wierd!use vars '$x'; sub li { $x = shift; @_ ? ($x, &li) : (); } print li qw(a b c d e f g); # prints gggggg
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: !@#$ $#_
by chromatic (Archbishop) on Mar 24, 2001 at 23:23 UTC | |
by MeowChow (Vicar) on Mar 25, 2001 at 00:51 UTC | |
by Dominus (Parson) on Mar 25, 2001 at 20:05 UTC |