cosmicperl has asked for the wisdom of the Perl Monks concerning the following question:
Hi All,
Ok, this has been bugging me for ages and I still haven't read or found anything to describe it. I've seen a few sub routines that have what looks like variable markers after them, today I came by the most complicated of which I've seen so far.
sub Request(;***$*$) {
I'm guessing this has something to do with the variables that are passed in?? But this example has a ;?? Eh? Can someone please give me a good explanation/description, or point me to the right docs.
Thanks
Lyle
Re: Sub routine ??? (This has been bugging me for ages)
by Somni (Friar) on Oct 13, 2007 at 02:53 UTC
|
Those are prototypes, documented in perlsub. They are used reduce code clutter and mimic the behavior of builtins by changing how the compiler parses subroutine calls.
Most Perl programmers new to prototypes instantly assume they're like other languages' prototypes, i.e. they are for declaring how many and what type of parameters a function expects. This is a misuse of them, as it generally causes problems and is quite often bypassed entirely anyways. See FMTEYEWTK about Prototypes. The article was long ago removed from O'Reilly's site, but it still remains relevant. | [reply] |
|
If Tom Christiansen's article indicated above hasn't convinced you, there is a somewhat shortened form of what it is saying in Damian Conway's "Perl Best Practices":
Don't use subroutine prototypes
Now, if that isn't clear enough.. :)
Chapter 9 of the PBP book, "Subroutines", has a section "9.10 Prototypes"; I think the book in it's entirety is worthwhile reading (actually, having it handy) if you enjoy doing things with Perl.
Good luck with those not-quite-friendly prototypes.
| [reply] |
Re: Sub routine ??? (This has been bugging me for ages)
by throop (Chaplain) on Oct 13, 2007 at 05:12 UTC
|
Your code contains prototypes. These allow
- function calls to be written with fewer parentheses, in some cases, and with a strong possibility of a loss of readability.
- function calls with fewer backslashes.
| [reply] |
|
Using prototypes force you to explicitly write each param instead of passing list containing correct params to subroutine... :-(
| [reply] |
|