Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: How do I prototype a function with a varying number of arguments?

by koolgirl (Hermit)
on Jul 29, 2011 at 18:55 UTC ( #917520=note: print w/replies, xml ) Need Help??


in reply to How do I prototype a function with a varying number of arguments?

I myself am doing the same exact thing as you, basically making my subroutines "generic", (at least any subroutine I write that could ever be used again) more for my own code library and the benefit of the practice than a module or anything, but I haven't really taken the approach of using prototypes. At the mention of making my subroutines generic, my guru suggested using them, but upon my own research, I just didn't see the benefit, seems a bit redundant to me, perhaps I'm missing their functionality/purpose?

Also, just food for thought, in doing this I've found it much easier to write a specific one time sub, then go back and change a few var names/etc to make it generic, than to write it generic the very first time through. I always feel a bit guilty about this, because I instinctively sense the waste of time and redundancy of writing something just to re-write it, but once you've got a huge library of very generic subs, it pays for itself many times over, and saves much more time than it wastes, IMHO anyway.

Oh, and JavaFan, very nice, I've never seen that in a sub prototype, ";" meaning an optional second scalar, I was about to suggest to lyapunov to just use an @ as the prototype, as your second example did, when I saw your reply and learned a new solution. ++

  • Comment on Re: How do I prototype a function with a varying number of arguments?

Replies are listed 'Best First'.
Re^2: How do I prototype a function with a varying number of arguments?
by JavaFan (Canon) on Jul 29, 2011 at 19:59 UTC
    I myself am doing the same exact thing as you, basically making my subroutines "generic", (at least any subroutine I write that could ever be used again) more for my own code library and the benefit of the practice than a module or anything, but I haven't really taken the approach of using prototypes. At the mention of making my subroutines generic, my guru suggested using them, but upon my own research, I just didn't see the benefit, seems a bit redundant to me, perhaps I'm missing their functionality/purpose?
    If there's any correlation between prototypes and genericness, it's a negative one. I'd say that prototypes make functions less generic.

    In general, I don't use prototypes, unless they give me a benefit at compile time. Prototypes I may consider using:

    • The empty prototype, for making constants.
    • The single scalar prototype, ($), as it allows for making unary name functions, which are parsed differently: foo bar $baz, $quux; depending on the prototype of bar, $quux is a parameter for foo or bar. Note that I mean a prototype consisting of only a $, and nothing else.
    • The give me $_ prototype, _. Have never used it so far.
    • The coderef prototype, &, so I can pass in a coderef as a bareblock, instead of having to use the sub keyword. I don't think I've ever used & in a prototype other than (&@).
    In all other cases, prototypes are much more of a hassle than they are worth it. And often, they are just plain annoying.

      Your words are almost exactly what went through my head when I first began the research into prototypes, behind the suggestion I mentioned.

      I'd say that prototypes make functions less generic.

      I completely agree, I mean how is it, that putting a limitation on the number and type of arguments that can be passed to a function, make it generic and more suitable to use as a standby function?...That concept never made much sense to me. Glad someone else saw that. Now, as far as the reasons one might choose to use prototypes, I'm sure there are many great arguments, but for the point of creating generic functions, I agree that there doesn't seem to be a correlation.

        ... how is it, that putting a limitation on the number and type of arguments that can be passed to a function, make it generic and more suitable to use as a standby function?

        How do you pass the elements of a two-item array to a function with the $$ prototype?

Re^2: How do I prototype a function with a varying number of arguments?
by lyapunov (Novice) on Jul 29, 2011 at 19:39 UTC

    Thank you all for the responses!

    JavaFan, thanks for the quick insight!

    AnomalousMonk, I have considered the question of using them, especially after reading Tom Christiansen's article but the reason that I use try to use them is that it sometimes helps me, in the same way using strict vars does. If I forget to feed a function call the needed requirements it lets me know. Truth be told, my learnedness and ability in Perl are below the threshhold where I think I will start seeing some of the bad juju.

    koolgirl, the reason that I use prototypes is two fold, with strict it helps me debug crappy little things like tranpsosition errors, and it has been forcing me to think harder about what I want to feed it. The article linked above is really well written, but as I said, for me the juice is worth the squeeze as it helps with accounting problems on my end.

    The other big push for me to build this library is that I am currently having to write a fair amount of code that does metrics from several different sources that need to be compiled together or have other special needs. I was beginning to see common refrains in the needed work. I do the write it once and revise, but I am enjoying the aspect of trying to write it in the most useful way possible first and see if I can anticipate unseen needs or problems and guard against them. Also, having seen a myriad of ways that people hobble together the same information, trying to anticipate those has been an even bigger pay off. In ways, I think this has helped me and my coding style quite a bit. But, I am boring geek, so I guess it goes with my territory.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://917520]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2022-08-08 04:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?