Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

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=perlquestion: print w/replies, xml ) Need Help??

flymolo has asked for the wisdom of the Perl Monks concerning the following question: (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.

Originally posted as a Categorized Question.

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

Replies are listed 'Best First'.
Re: How do I get both the return value and text? backticks vs. system()
by lhoward (Vicar) on Jun 21, 2000 at 02:00 UTC
    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;
Re: How do I get both the return value and text? backticks vs. system()
by Archon810 (Initiate) on Apr 30, 2008 at 01:42 UTC
    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?

Re: How do I get both the return value and text? backticks vs. system()
by jffry (Hermit) on Feb 14, 2006 at 23:34 UTC
    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://19119]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2021-04-19 08:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?