"be consistent" | |
PerlMonks |
Re: references and functionsby Errto (Vicar) |
on Aug 24, 2007 at 00:13 UTC ( [id://634760]=note: print w/replies, xml ) | Need Help?? |
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.
In Section
Seekers of Perl Wisdom
|
|