Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
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 all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2018-07-21 23:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (450 votes). Check out past polls.

    Notices?