Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

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 all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2018-01-17 21:01 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (206 votes). Check out past polls.