Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

USAGE OF Prototype

by abubacker (Pilgrim)
on Aug 06, 2009 at 13:27 UTC ( [id://786423]=perlquestion: print w/replies, xml ) Need Help??

abubacker has asked for the wisdom of the Perl Monks concerning the following question:

Can any one tell me what is the use of function prototypes in Perl . I know that is optional but I want to know that whether it has any use.

Replies are listed 'Best First'.
Re: USAGE OF Prototype
by dHarry (Abbot) on Aug 06, 2009 at 13:49 UTC

    It doesn't do what you think it does (Perl doesn't have named formal parameters, and yes I read your mind). Prototypes cause trouble and are best avoided. But to answer your question, prototypes allow you to change Perls usual argument-passing mechanism. It is an understatement to say this is tricky. If you want to validate arguments passed in you might want to take a look at Params::Validate. Also the PBP has a few good tips on how to use subs.

    HTH
    Harry

Re: USAGE OF Prototype
by si_lence (Deacon) on Aug 06, 2009 at 13:35 UTC
Re: USAGE OF Prototype
by Unforgiven (Hermit) on Aug 06, 2009 at 13:38 UTC
Re: USAGE OF Prototype
by ELISHEVA (Prior) on Aug 07, 2009 at 07:41 UTC

    The only really good use for prototypes is the creation of syntactic sugar. Perl prototypes can let you do neat things like write your own custom grep function that you can call like this:

    grepilicious {...} @someArray

    Prototypes can also provide limited parameter checking but there are a lot of gotchas involved. In fact, so many that the standard advice is not to use them that way. Here are two:

    • There is no guarantee that parameter checking will even take place. Perl only checks parameter prototypes when a subroutine is called as a function and has no leading &. If user decides to call your subroutine like $x->foo() or MyClass::X->foo() or even &foo($x,...), then Perl will happily ignore your prototype. A subroutine author has no control over how people call his/her code, the subroutine has to be written as if there were no error checking at all.
    • Even if parameter checking does take place, a prototype will only complain about bad parameters if it can't coerce the arguments you pass into good parameters. This can lead to strange and hard to track down bugs. For example, normally you would expect that foo(@someArray) would pass one parameter for each member of the array @someArray. So, for example, if @someArray=(1,2) then foo(@someArray) would be the same as passing foo(1,2)

      However, if you declare something with a prototype foo($;$) and then try to pass it foo(@someArray) or some other array, Perl will pass the size of @someArray, not the array elements. This is because the first $ of foo($;$) forces a scalar context on @someArray and an array in scalar context evaluates to its size.

    Protypes can also interfere with good programming practice if you aren't careful. Passing @_ is a good way to cleanly define two almost alike subroutines. It is a lot less prone to typos than extracting all the parameters and then passing them one by one to a subroutine that takes the same parameters:

    sub fooExtra { my $somethingExtra=shift; my $result=foo(@_); # combine $somethingExtra and $result in some way # and return, for example .... return $result + $somethingExtra; }

    But if you define foo($;$) with a prototype, the above code won't work. Instead of passing the parameters, you'll pass the size of @_, which is not at all what you wanted.

    Best, beth

Re: USAGE OF Prototype
by biohisham (Priest) on Aug 06, 2009 at 14:02 UTC
    prototypes can act as a check on the code to ascertain that function calls are performed properly, they indicate to Perl:
    • Types of arguments the function can take.
    • the number of arguments intended to be passed to the function.

    (updated)

    prototypes are flexible, you can also indicate to a function whether a certain prototype is optional and you can include references to hashes in there.
    example: sub NAME($$;$){...} #mistake corrected
    this function expects 2 scalar arguments and one optional argument after the ";"...so bear in mind that not all of this is irrelevant and there will come a time when you'd better use a prototype in a function definition than pass it over.
    Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.
      How would you go about inspecting the contents of a scalar to decide if it is a number using only prototypes?
      That sounds like more of a job for:
      confess("Parameter $n stinks") unless $param[$n] =~ /$goodRule[$n]/;

      Append:
      Example of extensive parameter handling:
      sub buyWidgets { my $numWidgets = shift; my $supplier = shift; #validate parameters unless ($numWidgets =~ /^\d+$/) { carp("'$numWidgets' is not a valid number of widgets: purchase + canceled\n"; return undef; } # Validate, and be flexible if possible. Accept both "ACME" and +"hash(0xPtrToACME)", and even ["ACME", "Bob's Widgets", "Black Market +"] if (ref($supplier) eq 'ARRAY') { my $buyCount = 0; foreach (@$supplier) { my $result = buyWidgets($numWidgets-$buyCount, $_); $buyCount += $result if defined($result); } return $buyCount; } if (ref($supplier) ne 'Supplier') { unless (exists $globalSupplierDirectory->{$supplier}) { carp("Cannot buy from '$supplier': not a valid supplier\n" +); return undef; } $supplier = $globalSupplierDirectory->{$supplier}; } # Do stuff with the good parameters return $supplier->buyWidgets($numWidgets); }
      Untested, and not self-contained.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-23 21:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found