Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: references and functions

by Errto (Vicar)
on Aug 24, 2007 at 00:13 UTC ( #634760=note: print w/replies, xml ) Need Help??

in reply to references and functions

1. The way to think of the \ in the prototype is "your parameter must be a real ..." So in this case, it has to be a real hash. Meaning, if you're calling this function, the first character of your argument had better be %. It can be %hash or %{ some_hash_ref_expr } or even %{'foo'} if you're not using strict, but it has to start with %. Then inside the body of your subroutine, what you get is a hash reference.

2. Prototypes only work when you call the function directly by name, as a list operator. They don't work when you call the subroutine by reference. So if you want to pass in a hash reference, you have to do it explicitly. I don't know the exact reason for this, but it's been an issue since prototypes appeared in Perl.

3. Same reason as #2. When you call the subroutine as a reference, the prototype doesn't apply so your arguments are no longer a hash per se, but a list of values that happen to have come from a hash.

4. $_[0]->{a} and $_[0]{a} are exactly equivalent. The second is just a shorthand in which the dereferencing arrow is inferred rather than explicitly written out. ${$_[0]}{a} would also be equivalent, but no one trying to write clear code would choose that on purpose. See perlref, note 3 under "Using References".

For further explanations on points 1-3 see perlsub under Prototypes.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2016-10-23 00:17 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (299 votes). Check out past polls.