in reply to USAGE OF Prototype

prototypes can act as a check on the code to ascertain that function calls are performed properly, they indicate to Perl:

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

Replies are listed 'Best First'.
Re^2: USAGE OF Prototype
by SuicideJunkie (Vicar) on Aug 06, 2009 at 15:36 UTC
    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.