Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

passing array of args to prototyped subs

by pldanutz (Acolyte)
on Sep 10, 2013 at 05:08 UTC ( [id://1053161]=perlquestion: print w/replies, xml ) Need Help??

pldanutz has asked for the wisdom of the Perl Monks concerning the following question:

sub f1 { return $_[0] + $_[1] } sub f2 ($$) { return $_[0] + $_[1] } my @a = (1, 2); print f1 (@a) . "\n" #print f2 (@a) . "\n" # compile-time error with f2
Is there no way to say that f2 expects 2 scalars, but should accept an array of arguments just like any other Perl function?

Replies are listed 'Best First'.
Re: passing array of args to prototyped subs
by afoken (Chancellor) on Sep 10, 2013 at 05:24 UTC

    You should really read perlsub. Perl prototypes do not work as you expect.

    In Perl, you do not check for the correct number and count of arguments at compile time, but at run time. The minimal solution for exactly two arguments is:

    sub f # *NO* prototype! { @_==2 or die "f needs two arguments"; # ... }

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      Fine. I was under the impression that Perl prototypes were still minimally useful. I expected the compiler to check that f2 has 2 scalar args WHEN FEASIBLE (e.g. when the call is f2 ($a, $b, $c)) and ignore the prototype when compile-time checking is unfeasible. I guess it's too much.

      Your link to a reply in another thread only proves that you can defeat compile-time prototype checking via using &f. however indirectly (which I knew)

        Prototypes are useful; they're just not useful for checking the number and type of arguments to a function.

        use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
        div class=
Re: passing array of args to prototyped subs
by boftx (Deacon) on Sep 10, 2013 at 05:24 UTC

    This might be a dumb question, but why do you need to use function prototypes to start with? If you are concerned about having correct argument types/numbers being passed in I'd just do some basic param checking in the function itself and catch it at runtime, which is probably where you want to have some sanity checks anyway.

      Because writing out the code to check arguments is longer and less convenient than writing f ($$), I guess?

      Yes, I realize it's only a few extra characters, I realize that this example is trivial (since I SIMPLIFY code before posting it), and I realize that the community doesn't seem to like prototypes (possibly for very good reasons)
        I realize that the community doesn't seem to like prototypes

        Well — not exactly. The Perl community is fine with prototypes when they’re used appropriately. The problem is, most of us come to Perl with a background in C/C++/Java, see Perl prototypes, and think, “That must be Perl’s way of doing argument checking.” But prototypes have little to do with argument checking; they are all about argument coercion.

        This is explained in detail in the classic article Far More than Everything You've Ever Wanted to Know about Prototypes in Perl -- by Tom Christiansen (written by the co-author of the Camel Book, no less). That article is a must-read for every Perl programmer.

        Hope that helps,

        Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

        I realize that the community doesn't seem to like prototypes.

        You probably referring, inter alia, to my comment of yesterday to another of your posts. Just to make things clear, I did not tell you not to use prototypes, I told you not to use prototype unless you really know how to do it and, more importantly, why you are doing it (and I might have added, unless you really know what they are doing). I personally use them only extremely rarely, but there have been few cases where I have found them handy.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1053161]
Approved by vinoth.ree
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (7)
As of 2024-04-23 15:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found