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).

In reply to Re: shelling out too much by graff
in thread shelling out too much by iblair

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.