Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
What is your good reason for using prototypes? (There are some.)

As far as I've learned there are few reason to use Perl prototypes. In my day to day life is normally sufficient to pass by reference or with named parameters, as you have done.
Prototypes are good only (in my humble experience) when you want bypass the flattening behaviour of @_, ie when you want to mimicry the behaviour of builtin function like push
# built in push @array, $first, $second, @all; # if you want to have something similar you have to sub my_push (\@@){ my $array_ref = shift; my @values = @_; ... } # so you can call it as my_push @arr, 'as', 'it was', 'a builtin';
The above is described in the Perl Cookbook in the chapter about subroutines.
Note: the syntax described in the perl cookbook seem a bit aged but still works:
perl -MConfig -e "print qq(For Perl $Config{version} the push prototyp +e is: ),prototype 'CORE::push' " For Perl 5.8.8 the push prototype is: \@@ perl -e " sub my_push (\@@){$x=shift;push @{$x},@_ }; my_push @arr, qw +(a,c,v); print @arr" a,c,v # BUT if you inspect manually the prototype of the original push on a +recent Perl you'll see perl -MConfig -e "print qq(For Perl $Config{version} the push prototyp +e is: ),prototype 'CORE::push' " For Perl 5.14.2 the push prototype is: +@
The prototype function is useful and produce the list provided in Prototypes section of perlsub.

As corallary of the no-flattening behaviour you can and must use prototypes when you want to pass subroutines as arguments to other subroutine, as you do with grep map and sort, for example.
my_grep (&@){ my $sub_ref = shift; @to_be_filterd = @_; ... }
Passing sub as arg is described in chapter 8 of Mastering Perl.

For complteness you MUST remember that "Calling a subroutine as &foo with no trailing parentheses ignores the prototype of foo and passes it the current value of the argument list, @_" as stated in the FAQ What's the difference between calling a function as &foo and foo()?

I see no reason to use prototypes in your case. In the official docs is stated about prototypes: Because the intent of this feature is primarily to let you define subroutines that work like built-in functions,...


There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

In reply to Re^4: Function Prototypes - the only reasons I know by Discipulus
in thread Function Prototypes by KimberTLE

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2022-05-17 04:16 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (65 votes). Check out past polls.