The Perl 5 prototypes mechanism seems to be somewhat deprecated, at least within the commnity right now. We had something of a discussion in the CB this week and Far more than you ever wanted to know about prototypes has a lengthy, and substantial discussion about prototypes.

TheDamian in his recent tome, Perl Best Pratices explains on pp194-196 just what can go wrong with code using prototypes and why. When I first started using Perl I used prototypes, for, oh, about the first week! I had come from 'C' and expected that this was how things should be done. My problem was the one pointed out on p195. If a prototype is given thus (stolen shamelessly from shmem's reply

#!/usr/bin/perl sub getInfo ($\%) { my $scalar = shift; my $hash = shift; print "scalar = '$scalar'\n"; print "hash = $hash\n"; print "$_ => $hash->{$_}\n" for keys %$hash; } my %hash = (foo => 'bar', baz => 'quux'); my $scalar = 'string'; getInfo($scalar,%hash); __END__
Will give the desired result. BUT there is a problem of magic being perpetrated here, the call to the sub,
getInfo($scalar,%hash);
implies that the hash is going to be "squashed" into the list context and unless you are aware that a prototype was being used you would expect to see
my ( $thescalar, %thehash ) = @_;
in the sub. The call to the prototyped function doesn't pass the hash at all! It passes a reference, but that fact is obscured. So by looking at the subroutine call we dont really know what the subroutine is doing.

If we want to pass a hash reference, why not do it explicitly:

sub getInfo { my ( $thescalar, $thehashref ) = @_; .... } getInfo( $scalar, \%hash );
at least then everybody will know that the subroutine is expecting a hash and not a hash reference by some magic. Then if you want the hash itself to be copied in:
sub getInfo { my ( $thescalar, %thehash ) = @_; .... } getInfo( $scalar, %hash );
and then the subroutine definition and its invocation actually look alike! But don't believe me, search the Monastery on prototypes and read TheDamian.

jdtoronto


In reply to Re: Parsing a hash into a sub by jdtoronto
in thread Parsing a hash into a sub by RaginElmo

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.