in reply to Setting defaults in subroutines

You're being burnt by a combination of operator precedence and context.

my @index = @{$args{Index}} || ();

is interpreted as

my @index = (@{$args{Index}} || ());

Then the || operator forces scalar context on @{$args{Index}} which therefore gives the number of elements in the array (which is 2).

On the other hand,

my @index = @{$args{Index}} or ();
is interpreted as
(my @index = @{$args{Index}}) or ();

So the array is evaluated in list context and everything works as expected.

--
<http://www.dave.org.uk>

"The first rule of Perl club is you do not talk about Perl club."
-- Chip Salzenberg

Replies are listed 'Best First'.
Re^2: Setting defaults in subroutines
by rbi (Monk) on Aug 04, 2004 at 09:54 UTC
    Pretty clear explaination. Thank you very much!
      The best way to see the precedence is to have a look at the cheat sheet, which shows this. I got confused with this myself a couple a weeks ago.

      Hans