Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Simplifying repeated parameter lists

by roboticus (Canon)
on Sep 19, 2012 at 20:02 UTC ( #994519=note: print w/ replies, xml ) Need Help??


in reply to Simplifying repeated parameter lists

Voronovich:

I just had another amusing thought: Use a closure to hold your subroutine call with some of the arguments in place. It's a little more complicated, but if you've got multiple functions and/or sets of common arguments, I think it's a win::

$ cat closure_call.pl #!/usr/bin/perl use strict; use warnings; # # Stuff you already have in your program # my ($handle, $not_today, $thingie) = qw($handle $not_today $thingie); sub printargs { print "printargs(", join(", ", @_), ")\n"; } # Get a reference to a function with some of the args already in place my $simple = simplificate(\&printargs, $handle, $not_today, $thingie); my $simpl2 = simplificate(\&printargs, "tittle", "tattle"); + # Function to build a reference with some arguments already in place sub simplificate { my ($rFn, @args) = @_; return sub { &$rFn(@args, @_) }; } # Now call it as you need it + &$simple("Foo!", 234); &$simple("Frob!",23); &$simple("Forb!",54); &$simple("ooF!",11); &$simple("broF!", 'fakenum'); &$simple("Frap!",458); &$simpl2("Whackmatic"); &$simpl2("Cybernicus"); &$simpl2("Canem Infernae"); $ perl closure_call.pl printargs($handle, $not_today, $thingie, Foo!, 234) printargs($handle, $not_today, $thingie, Frob!, 23) printargs($handle, $not_today, $thingie, Forb!, 54) printargs($handle, $not_today, $thingie, ooF!, 11) printargs($handle, $not_today, $thingie, broF!, fakenum) printargs($handle, $not_today, $thingie, Frap!, 458) printargs(tittle, tattle, Whackmatic) printargs(tittle, tattle, Cybernicus) printargs(tittle, tattle, Canem Infernae)

Update: Darn! BrowserUk beat me to the punch!

Update: Forgot to update code for simpl2.

...roboticus

When your only tool is a hammer, all problems look like your thumb.


Comment on Re: Simplifying repeated parameter lists
Download Code
Re^2: Simplifying repeated parameter lists
by Voronich (Hermit) on Sep 19, 2012 at 20:14 UTC

    I'm not gonna lie, my reaction was a healthy combination of "Neat! wait...why?"

    I'm keeping that in the bag of tricks, 'cause I think if I go spend some time in the machine shop I'll get hit with a "THAT'S why!".

      Voronich:

      Admittedly, it's probably more "neat-o!" than practical. (Though it's just useful enough for me to drop into my bag-o-tricks module for general use.) It probably only pulls its weight when you have multiple sets of default arguments and/or multiple different functions you want to decorate.

      There might be a few useful variations, too. For example, might want to make a bunch of default arguments at the *end* of your list (such as for adding formats to cells using Spreadsheet::WriteExcel.

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.

        yeah the more I reread that the more it becomes that it's going to make it in to my code before too terribly long.

        for instance: the "submit_thingamabob" function is a nasty piece of databasocity. If I could reasonably swap that call out and replace it with a mock, without mucking too much with the existing code, it would speed development remarkably.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2014-08-30 16:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (293 votes), past polls