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 reply to Re: references and functions by Errto
in thread references and functions by jignasu

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.