On Thu, 17 Mar 2005 09:32:43 -0500 (EST), Chris Devers <[EMAIL PROTECTED]> wrote: > On Thu, 17 Mar 2005, Ramprasad A Padmanabhan wrote: > > > > Okay, so we're back to my other suggestion -- "require" it: > > > > > > { > > > $script = get_name_of_script(); # names matter! pick good ones! > > > $output = require $script or > > > die "Couldn't 'require' $script\n$!\n"; > > > do_something($output); > > > } > > > > > > > No I cant use require. Let me tell you why. > > ...maybe that would have been helpful at the outset :-) > > > for eg > > > > { > > $recipient = "[EMAIL PROTECTED]"; # This will come from the MTA > > $scriptname = get_scriptname($recipient); > > > > # if I do require here > > require "$scriptname"; > > > > # HERE LIES THE PROBLEM > > somefunc($arg1,$arg2,$arg3) # somefunc is defined in $scriptname > > } > > Okay, so now we're back to my other original suggestion -- use a module: > > { > $recipient = "[EMAIL PROTECTED]"; # This will come from the MTA > $scriptname = get_scriptname($recipient); > > use $scriptname qw( somefunc ); > > somefunc($arg1,$arg2,$arg3) # somefunc is defined in $scriptname > } > > And now you just have to see that $scriptname exports somefunc(). > > If this gets called a lot, you may need to wrap it in an eval() so that > the namespace doesn't get clobbered. (I think, I'm a little confused as > to what would happen in that case, but it seems like an eval should > smooth out many glitches...). > > > Also I cannot go on requiring files again and again, my process will > > hog all the memory then. > > Well, yes, but the way you've designed this, you already run that risk. > > Now if you replaced get_scriptname() with get_subroutine(), and found a > way to abstract out the bits that are different for each $recipient, > then you could simplify things tremendously, and hopefully make your > resource needs go way down: > > { > $recipient = "[EMAIL PROTECTED]"; # This will come from the MTA > $sub_name = get_subroutine($recipient); # do this in one step > > $sub_name->($arg1,$arg2,$arg3); > } > > This will do everything without making any external calls. > > -- > Chris Devers
I take it the issue here is that his users have scripts (looks like mail filters?), and he wants to write a script that will iterate through them all and run them using parameters he provides. If every user has a customized script and there are many users, rewriting all the scripts as modules would be impractical. Apparently users share scripts, but he's still indicating that there is a potentially large number of scripts. And if he did rewrite them as modules, it would solve his issue, but break them for the users, unless he renamed them and provided scripts with the existing names as interfaces to the new modules. This is a nasty little situation. My quick and dirty solution--while I sat down and made some policy decisions to improve my programming life--would be the following: 1) Read the external scripts into variables. Storing them in a hash declared outside the loop would make the most sense, so that I could hang onto them for later use. Using DB_File or storing them to temp files and keeping the hash as an index of paths would save on memory; I leave the implementation as an exercise for the reader. 2) Within the loop, once I've fiured out which script I'm going to use: my $loopscript = $scripts{the_one_I_need} ; my ($loadargv1, $loadargv2, $loadargv3) = ($var1, $var2, $var3) ; $loopscript =~ s/^(.+)/[EMAIL PROTECTED] = \($loadargv1, $loadargv2, $loadargv3\);/; exec $loopscript ; Or something to that effect. The above is untested, but it should work. HTH, --jay -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>