Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Perl Prototypes

by djantzen (Priest)
on Nov 02, 2002 at 09:34 UTC ( [id://209934]=note: print w/replies, xml ) Need Help??


in reply to Perl Prototypes

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'.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://209934]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-25 07:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found