Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://375198]
Happy-the-monk .o0O(j'en ai marre de vos Connerys.)
[marto]: Yesch!
[Happy-the-monk]: s/j'en/Sean/g;

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (11)
As of 2018-04-23 10:36 GMT
Find Nodes?
    Voting Booth?