Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem is that Perl's function prototypes don't do what people think they do. Their purpose is to allow you to write functions that will be parsed like Perl's built-in functions.</p> <p>First of all, method calls completely ignore prototypes. If you're doing OO programming, it doesn't matter what prototype your methods have. (So they shouldn't have any prototype.)</p> <p>Second, prototypes aren't strictly enforced. If you call a subroutine with <code>&amp;function(...)</code>, the prototype is ignored. So they don't really provide any type safety.</p> <p>Third, they're spooky action-at-a-distance. (Especially the <code>$</code> prototype, which causes the corresponding parameter to be evaluated in scalar context, instead of the default list context.)</p> <p>In particular, they make it hard to pass parameters from arrays. For example:</p> <pre><code>my @array = qw(a b c); foo(@array); foo(@array[0..1]); foo($array[0], $array[1], $array[2]); sub foo ($;$$) { print "@_\n" } foo(@array); foo(@array[0..1]); foo($array[0], $array[1], $array[2]); </code></pre> <p>prints:</p> <pre><code>a b c a b a b c 3 b a b c </code></pre> <p>along with 3 warnings about <code>main::foo() called too early to check prototype</code> (if warnings are enabled). The problem is that an array (or array slice) evaluated in scalar context returns the length of the array.</p> <p>If you need to write a function that acts like a built-in, use a prototype. Otherwise, don't use prototypes.</p> <p>Note: Perl 6 will have completely revamped and very useful prototypes. This answer applies only to Perl 5.</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. 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