Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

How do I get both the return value and text? backticks vs. system()

( #19119=categorized question: print w/ replies, xml ) Need Help??
Contributed by flymolo on Jun 21, 2000 at 01:59 UTC
Q&A  > programs and processes


Description:

`some command` will give me the text of the executed program. system("command") will return the return value, but running the program twice might change text and/or return values. Is there any easy way to do this? Right now I'm thinking system("command >file") but then I'd have a file I don't need.

Answer: How do I get both the return value and text? backticks vs. system()
contributed by lhoward

open has a piping syntax that you can use to pipe data to or from a program. You can get the exit value from $? (see perlvar).

open P,"command |" or die "error running command $!"; my @data=<p>; close P; my $exit_value=$? >> 8;
Answer: How do I get both the return value and text? backticks vs. system()
contributed by Archon810

Having used the new Perl 5.10, I was shocked to find this new variable after months of use:

${^CHILD_ERROR_NATIVE}
This variable gives the native status returned by the last pipe close, backtick command, successful call to wait() or waitpid(), or from the system() operator. See perlrun for details. (Contributed by Gisle Aas.)

New internal variables

Finally!.. what else can I say?

Answer: How do I get both the return value and text? backticks vs. system()
contributed by jffry

If you especially don't want the shell interpreting shell metacharacters, then don't pass the command to the shell, use the 4+ argument form of open.

open(CMD, '-|', '/usr/bin/ls', '$4', '$PATH'); my $output = do { local $/; <CMD> }; close CMD; print "$output\n";
Here is the output, you'll see that the "$4" and "$PATH" were not interpreted as variables before being sent to ls.
ls: 0653-341 The file $4 does not exist. ls: 0653-341 The file $PATH does not exist.

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (10)
    As of 2014-11-26 17:40 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My preferred Perl binaries come from:














      Results (172 votes), past polls