First off, the prototype of fun1 is forcing scalar context upon the arguments passed in. This is why you're getting '4' instead of a populated array 4 elements long.

As to the other questions, since 95% of my Perl code is object-oriented, and because methods* in contrast to functions ignore prototypes, I don't generally use prototypes. The reason prototypes are ignored in OO Perl is that at compile time -- which is when prototypes are checked -- perl doesn't know which module in an object hierarchy contains the method that will ultimately be called, as method dispatch occurs at runtime (see TheDamian's Object Oriented Perl page 170 for more). Note as well that in strongly typed languages like Java the prototype is actually part of the method signature, such that public int foo(String str) is a different method from public int foo(boolean bool) even if both appear in the same class or object hierarchy**. For a brief time I uselessly utilized prototypes in OO Perl, and continued with them very briefly for documentation purposes. I've found it's better generally just to write good documentation though, rather than write extra code in the hope someone gets my meaning ; )

As a general rule, avoid using the ampersand format for calling functions. What it does is override the default behavior of @_, so it ought only be used when you're sure that's what you want to do. Instead, use parentheses after the method name to indicate that something is a subroutine.

Good reading: Are prototypes evil?, Re (tilly) 4: optimized switch case

*Note: when using a method (SomePackage->doSomething, or $some_obj->doSomething) as opposed to a function (SomePackage::doSomething) the first parameter in the argument stack (@_) is the package name or object reference.

**The term for such methods is 'overloaded', as opposed to 'overridden'.


In reply to Re: Perl Prototypes by djantzen
in thread Perl Prototypes by hawtin

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.