in reply to shelling out too much
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):
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).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"; }
|
|---|