Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Setting defaults in subroutines

by davorg (Chancellor)
on Aug 04, 2004 at 09:43 UTC ( #379947=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://379947]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2018-10-20 17:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When I need money for a bigger acquisition, I usually ...














    Results (119 votes). Check out past polls.

    Notices?