Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Prototypes aren't bad if used correctly. The difficulty is that Perl's prototypes don't work the way people often expect them to. People with a background in other programming languages tend to expect prototypes to provide a mechanism for checking that function calls are correct: that is, that they have the right number and type of arguments. Perl's prototypes are not well-suited for this task. It's the <em>misuse</em> that's bad. Perl's prototypes have a singular and very different purpose:</p> <p><strong>Prototypes allow you to define functions that behave like built-in functions.</strong></p> <ul> <li>Parentheses are optional.</li> <li>Context is imposed on the arguments.</li> </ul> <p>For example, you could define a function like this:</p> <pre><code>sub mypush(\@@) { ... } </code></pre> <p>and call it as</p> <pre><code>mypush @array, 1, 2, 3; </code></pre> <p>without needing to write the <code>\</code> to take a reference to the array.</p> <p>In a nutshell, prototypes let you create your own syntactic sugar. For example the Moose framework uses them to emulate a more typical OO syntax.</p> <p>This is very useful but prototypes are very limited:</p> <ul> <li>They have to be visible at compile-time.</li> <li>They can be bypassed.</li> <li>Propagating context to arguments can cause unexpected behavior.</li> <li>They can make it difficult to call functions using anything other than the strictly prescribed form.</li> </ul> <p>See <a href="http://perldoc.perl.org/perlsub.html#Prototypes" rel="noreferrer">Prototypes</a> in perlsub for all the gory details.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload