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

Re: Capturing both STDOUT, STDERR and exit status

by polettix (Vicar)
on May 06, 2005 at 16:31 UTC ( [id://454724]=note: print w/replies, xml ) Need Help??


in reply to Capturing both STDOUT, STDERR and exit status

A backtick/qx suffices to mimic what you already do:
my $alloutput = qx(/bin/ls / 2>&1); my $exitcode = $? >> 8;
If you need to keep stderr and stdout separate use merlyn's suggestion.

Flavio (perl -e 'print(scalar(reverse("\nti.xittelop\@oivalf")))')

Don't fool yourself.

Replies are listed 'Best First'.
Re^2: Capturing both STDOUT, STDERR and exit status
by pbeckingham (Parson) on May 06, 2005 at 17:54 UTC

    Thanks, this is nice and clean. I did make the qx output scalar, otherwise it gives me a list of output lines.

    sub executeCommand { my $command = join ' ', @_; ($? >> 8, $_ = qx{$command 2>&1}); } my ($status, $output) = executeCommand ('/bin/ls', '/');



    pbeckingham - typist, perishable vertebrate.
      You have to reverse the return list, otherwise you'll refer to the previous value of $?:
      sub executeCommand_wrong { my $command = join ' ', @_; ($? >> 8, $_ = qx{$command 2>&1}); } sub executeCommand_correct { my $command = join ' ', @_; ($_ = qx{$command 2>&1}, $? >> 8); } my $command = 'echo -n ciao ; false'; my ($status, $output) = executeCommand_wrong ($command); print "[$output] -> [$status]\n"; ($output, $status) = executeCommand_correct($command); print "[$output] -> [$status]\n"; __END__ [ciao] -> [0] [ciao] -> [1]
      If you cannot live without having $status as the first returned value, just use reverse:
      sub executeCommand { my $command = join ' ', @_; reverse ($_ = qx{$command 2>&1}, $? >> 8); }

      Flavio (perl -e 'print(scalar(reverse("\nti.xittelop\@oivalf")))')

      Don't fool yourself.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-19 13:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found