Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^4: Function Prototypes - the only reasons I know

by Discipulus (Monsignor)
on Nov 24, 2015 at 08:58 UTC ( #1148484=note: print w/replies, xml ) Need Help??


in reply to Re^3: Function Prototypes
in thread Function Prototypes

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

HtH
L*

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1148484]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2018-06-18 01:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?



    Results (107 votes). Check out past polls.

    Notices?