Re: shelling out too much
by Joost (Canon) on Jun 27, 2005 at 12:11 UTC
|
requireing a script that isn't coded for that situation could override all kinds of data and subroutines in your calling code. also, require will only load a file once. if you are sure that it won't have any strange side-effects, you could probably do
@ARGV = '-h';
do 'myscript.pl';
However, since that will recompile the code each time, you will not gain optimum performance.
Your best strategy would be to put the shared code in a module and use that in both scripts. See perlmodlib.
| [reply] [d/l] |
|
|
{
local @ARGV = '-h';
do 'myscript.pl';
}
--
We're looking for people in ATL
| [reply] [d/l] [select] |
|
|
@ARGV = '-h';
do 'myscript.pl';
Joost, I too thought of proposing something like this, but then I realized that if the OP is using backticks, he/she must be collecting the standard output of the child process, which neither do nor require would yield.
| [reply] [d/l] |
|
|
Hmmm.. you could use something like *
my $s = open(APP,"-|")
if ($s) {
local @ARGV = '-h';
do 'myscript.pl';
exit;
}
else {
die "Can't fork: $!" unless defined $s;
local $/;
my $scriptoutput = <APP>;
close APP;
}
But that's probably just making it too complex.
* completely untested code.
| [reply] [d/l] |
|
|
tlm, just because they're using backticks doesn't mean that they care about the output. It should mean that, but it doesn't necessarily mean that. I've seen many occurances, both here at perlmonks and at work, where people use backticks in void context as a short-cut to running system.
Confusing the situation just a bit more is how the OP talks about possibly using require instead. Now, one possibility is that the OP doesn't need backticks, the other is that require wouldn't fit the bill, even if the "-h" parameter went away.
| [reply] |
Re: shelling out too much
by samizdat (Vicar) on Jun 27, 2005 at 12:45 UTC
|
Or, learn to fork, p.147 in the llama book. You integrate both programs into one, and spin off the portion which does the secondary processing.
As a second, suggestion, if you use
system('myscript.pl', '-h');
there's less shelling. :D | [reply] [d/l] |
Re: shelling out too much
by graff (Chancellor) on Jun 28, 2005 at 02:18 UTC
|
Obviously I could use `myscript.pl -h`; to do this, but shelling out slows my script down considerably!
Is this true because you are running "myscript.pl -h" lots of times in a loop? I would assume so, based on the node title, along with the fact that the overhead involved in a single back-tick sub-shell would be hard to notice, whereas doing this in a loop thousands (or even just hundreds) of times would be very noticeable.
In my own experience, a lot of that overhead comes from invoking and cleaning up after the sub-shell. If you really are using backticks in a void context (not assigning the output of the sub-shell to a variable), you could start a shell just once, and then run the perl script as many times as you like (but only if you're using some sort of *n*x):
open( SH, "| /bin/sh" ) or die "Can't open /bin/sh: $!";
# whatever you loop construct is...
{
# assign command line args to @args
# ... CAREFULLY! (watch out for shell "magic" characters,
# escape them as needed)
print SH "myscript.pl -h @args\n";
}
This might be better than forking, if you're doing a lot of iterations; it is definitely better than consecutive backticks (but I haven't tried to benchmark it against all the alternatives). | [reply] [d/l] |
Re: shelling out too much
by zentara (Cardinal) on Jun 28, 2005 at 11:13 UTC
|
What do you mean "slows down your script"? If you are running it thru system or backticks, your script will have to wait for it to complete, before continuing. If "myscript" involves a long process, then you just have to wait. It wouldn't be any faster if you put the guts of "myscript" into a subroutine into your original script. You will still have to wait for that subroutine to do it's work and return. The amount of time it takes for Perl to compile the script is neglible, unless you are repeatedly calling it 100's of times.So what are your options? If you don't need "return values" from "myscript" (like if it is mailling something), you can do a "fork-and-exec" to run it, which will let your main program continue running at full speed. The tricky part, is if you need return values. In that case, there is IPC, POE and GUI's which use event loops like Tk, Gtk2, Wx , etc. You can also setup some pipes and pipe your data back from a fork and exec. Since you claim your problem is the -h option, preventing a require, why not make a
copy of "myscript" and call it myscript1, and just hardwire the -h option into it, and require it? I have a feeling that it won't fix your problem, because it involves a time consuming process.
I'm not really a human, but I play one on earth.
flash japh
| [reply] |