... is there a way to require a reference in the signature?

The  \@ \% \& prototypes cause references to the given types of data to be taken and passed to a subroutine. In a sense, you can say this is "requiring a reference."

c:\@Work\Perl\monks>perl -wMstrict -le "sub foo (\%\@) { my ($hashref, $arrayref) = @_; ;; print ref $hashref; print ref $arrayref; ;; print qq{spanish for 'two' is '$hashref->{two}'}; print qq{@{ $arrayref }}; } ;; ;; my @ra = qw(fee fie foe fum); my %hash = qw(one uno two dos three tres); ;; foo(%hash, @ra); " HASH ARRAY spanish for 'two' is 'dos' fee fie foe fum
(See what happens if you use a hash in place of an array, etc.)

Of course, in a call to a "normal" (i.e., unprototyped) function like  no_proto(%hash, @array) all the elements of the hash(es) and array(s) would be "flattened" in the function argument list, and there would be no way of telling where one left off and the next began.

I use subroutine signatures when I can ...

I can understand why someone who comes from a background in C, C++ or other strongly typed language would feel very comfortable with Perl Prototypes, and think that they understood them as soon as they saw them. I certainly did. But Perl prototypes are a different beast, and in time you may see your sense of understanding and comfort melt away. Please see Far More than Everything You've Ever Wanted to Know about Prototypes in Perl -- by Tom Christiansen for a number of reasons not to use prototypes on a regular basis, but only in rather specialized circumstances — where they can, indeed, sometimes be very useful!


Give a man a fish:  <%-{-{-{-<


In reply to Re^4: Tutorial RFC: Guide to Perl references, Part 1 by AnomalousMonk
in thread Tutorial RFC: Guide to Perl references, Part 1 by stevieb

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.