Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

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
    choroba waves
    [Mj1234]: Hello Monks, I have installed Active perl 5.16 and 5.14 but I dont find Devel::GDB package when I try to install through ppm
    [Discipulus]: i'v installed ubuntu 16.04lts and i'm quite disappointed: is deadly simmilar to new win osses where you cannot find even simple things...
    [Corion]: Hi Discipulus ;)
    [Corion]: Mj1234: Maybe Ubuntu didn't package Devel::GDB? In that case, consider using local::lib and install the module from CPAN
    [Corion]: But a quick Google search turns up this page, which seems to suggest that it should be packaged
    [Discipulus]: iirc active perl has repositories only for last 2 o 3 major release of perl Mj1234. I strongly suggest portable version of strawberry perl and use cpan client to install modules
    [choroba]: Also, who needs GDB when there's hdb?

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (11)
    As of 2016-12-07 08:53 GMT
    Find Nodes?
      Voting Booth?
      On a regular basis, I'm most likely to spy upon:

      Results (125 votes). Check out past polls.