Max_Glink,

Coincidentally I just came across some code on a new project that speak to the issue of "what method to use to execute a unix command".

The code needed to get the output from an in-house employee lookup program and return some of the values to an HTML page.

The method being used was using "system" and piping the command output to a tempfile, then opening the tempfile and getting the data back out for display. Although functional, it was not especially efficient.

My alternative was to use "backticks" for the command, which allowed me to capture the output to an array, which I then passed to the parser than was managing the array created by the file read.

Here are the two methods.

# assume $lname, $tempfile were assigned previously my $command = "unix_blues -w $lname >> $tempfile"; my $rc = system($command); # the above puts the output into $tempfile and the return code in $rc # they then open, read the tempfile putting lines into array (which co +uld have # also been done in one line)

One problem with the above was that for whatever reason, they occasionally had tempfiles lying around from failed unlinks.

So I changed it to this, which resolved the tempfile issue and removed the file I/O

my $command = "unix_blues -w $lname"; @BluesData = `$command`;

the above put the output lines into @BluesData which I then passed to an existing routine that parsed it.

It sped things up considerably.

Just more proof TIMTOWTDI!

Good luck!


In reply to Re: calling Unix commands by wardk
in thread calling Unix commands by Max_Glink

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.