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

If you come from a strictly typed language like C++, prototypes seem like a good idea. In Perl almost always they aren't. Which is why mentioning prototypes in Perl often elicits a "Don't do that!" response.

What is your good reason for using prototypes? (There are some.)

Your sample non-prototype code looks pretty good to me because you have effectively named the parameters where they are being passed. That is hugely better documentation for someone reading the code later than simply passing a list and hoping for the best. Perl prototypes don't give type safety - they tend to enforce type matching which puts them on a level with C style casts. See Prototypes for the full glory of function prototyping.

Premature optimization is the root of all job security

Replies are listed 'Best First'.
Re^4: Function Prototypes - the only reasons I know
by Discipulus (Abbot) on Nov 24, 2015 at 08:58 UTC
    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.