Clear questions and runnable code
get the best and fastest answer
Re^4: Function Prototypes - the only reasons I knowby Discipulus (Monsignor)
|on Nov 24, 2015 at 08:58 UTC||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
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:
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.
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.