Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Perl Monk, Perl Meditation
 
PerlMonks  

Re^3: Passing a hash, and a scalar to subroutine

by hbo (Monk)
on Jul 17, 2004 at 05:48 UTC ( #375198=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Passing a hash, and a scalar to subroutine
in thread Passing a hash, and a scalar to subroutine

A hash in list context, such as a subroutine argument list, gets converted to a list of key,value pairs. So you are actually passing three arguments in your example code. One is the scalar, and the key and value of the single hash entry. Inside the sub, you convert the argument list back into a hash. The rule here is that the elements in the list are entered into the hash as key1 => value1, key2 => value2 and so forth. (The "=>" is just another way of writing "," but it emphasizes the pairing between key and value.) So your three elements get assigned into the hash as scalar => key1, value1 => undef, which makes a "hash" out of the whole deal. Since you only have three elements, Perl helpfully supplies the undef value as the fourth value in the list. Doing the shift removes the scalar from the argument list and leaves two elements, which assign back into the hash as you desire.


Comment on Re^3: Passing a hash, and a scalar to subroutine

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2014-04-19 10:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (480 votes), past polls