Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

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

by flymolo (Acolyte)
on Jun 21, 2000 at 01:59 UTC ( #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


`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:

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!
  • 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, 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
  • 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.
  • Log In?

    What's my password?
    Create A New User
    [shmem]: Discipulus: has that been in the CB? or in a PM node proper? for the latter case, can you provide the ID?
    [Discipulus]: you seriously can use their in this way in eng?
    [Corion]: Meh. $effin_bad_system has an interface breakdown and then loads events in parallel with events overtaking one another instead of being processed sequentially
    [shmem]: Discipulus: dunno, but we do all the time ^^
    [choroba]: Discipulus I was taught so by a Londoner
    [shmem]: Corion: very clear case of missing sequence number
    [Corion]: shmem: Yeah. I guess they have a sequence number but distribute the events across threads or machines or whatever.
    [karlgoethebier]: choroba: another chapter of "Learning English At The Monastry"?
    [shmem]: Corion, well then... next issue, sequence number not a shared resource :P
    [Discipulus]: shmem i'm searching it.. but failing i was sure was in Re: Let's Make PerlMonks Great Again! -- suggestions and dreams

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (14)
    As of 2017-05-23 08:23 GMT
    Find Nodes?
      Voting Booth?