Is it better to avoid prototyping here, or to prototype like...

It's best to avoid prototyping altogether unless you know what it does. In C a prototype tells the compiler, as well as anyone reading the code, what type of arguments a function expects and what type of value it returns. That is not what Perl prototypes do and not what they're for.

Consider Perl's built-in 'push' function. It takes an array followed by a list of one or more scalars:

push ARRAY, LIST

If you tried to write a similar function yourself in Perl you might start it like this:

sub my_push { my(@array, @list) = @_; ...

But that's not going to work for two reasons:

  1. @array is now a copy of the array that was passed to your function, so nothing you do to it will affect the original
  2. the arguments to my_push have been flattened into a single array so all the values end up in @array and @list ends up empty

Perl prototypes let you write subroutines that work the same way Perl functions do. They let you implicitly take a reference to the calling arguments and they let you avoid the problem of everything being flattened to a single array. A working version of my_push might start like this:

sub my_push (\@@) { my($array_ref, @list) = @_; ...

There are lots of subtleties with using prototypes (eg: in our example we do want the list of scalars to be flattened - hence no leading backslash on the second '@') so if you don't know what you're doing you can create problems that are hard to debug.

It's unfortunate that this facility in Perl is called 'prototypes' when it really means an entirely different thing than what you might be familiar with that term meaning.

Short story: if you're not trying to write subroutines that work like Perl's built-in functions then don't use prototypes at all.


In reply to Perl Prototypes (was Re: XML::Parser Weirdness) by grantm
in thread XML::Parser Weirdness by Anonymous Monk

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.