Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Function Prototypes

by Anonymous Monk
on Nov 24, 2015 at 01:32 UTC ( [id://1148463]=note: print w/replies, xml ) Need Help??


in reply to Function Prototypes

IMHO, it should look like this:
# the line above was intentionally left blank
In other words, don't use prototypes at all...

Replies are listed 'Best First'.
Re^2: Function Prototypes
by KimberTLE (Initiate) on Nov 24, 2015 at 01:56 UTC
    Thank you for your "In other words, don't use prototypes at all..." quip. It was certainly useful, and I shall cherish it for all time! Now, why do you think I asked for assistance with how to code the prototypes? Do you think it's because I don't like prototyping, or do you think it's because I prefer prototyping? I'll give you a few seconds to consider your answer...

      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
        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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1148463]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (None)
    As of 2024-04-25 03:56 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found