<?xml version="1.0" encoding="windows-1252"?>
<node id="994519" title="Re: Simplifying repeated parameter lists" created="2012-09-19 16:02:04" updated="2012-09-19 16:02:04">
<type id="11">
note</type>
<author id="533863">
roboticus</author>
<data>
<field name="doctext">
&lt;p&gt;[Voronovich]:&lt;/p&gt;
&lt;p&gt;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::&lt;/p&gt;
&lt;c&gt;
$ 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(\&amp;printargs, $handle, $not_today, $thingie);
my $simpl2 = simplificate(\&amp;printargs, "tittle", "tattle");                         

# Function to build a reference with some arguments already in place
sub simplificate {          
    my ($rFn, @args) = @_;  
    return sub { &amp;$rFn(@args, @_) };
}                           
                            
# Now call it as you need it                                                          
&amp;$simple("Foo!", 234);      
&amp;$simple("Frob!",23);       
&amp;$simple("Forb!",54);       
&amp;$simple("ooF!",11);        
&amp;$simple("broF!", 'fakenum');      
&amp;$simple("Frap!",458);      
  
&amp;$simpl2("Whackmatic");
&amp;$simpl2("Cybernicus");
&amp;$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)
&lt;/c&gt;
&lt;p&gt;&lt;b&gt;Update:&lt;/b&gt; Darn! [BrowserUk] [node://994517|beat me to the punch!]&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Update:&lt;/b&gt; Forgot to update code for simpl2.&lt;/p&gt;
&lt;p&gt;...[roboticus]&lt;/p&gt;
&lt;p&gt;&lt;i&gt;When your only tool is a hammer, all problems look like your thumb.&lt;/i&gt;&lt;/p&gt;</field>
<field name="root_node">
994506</field>
<field name="parent_node">
994506</field>
</data>
</node>
