Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re^2: Capturing both STDOUT, STDERR and exit status

by pbeckingham (Parson)
on May 06, 2005 at 17:54 UTC ( #454744=note: print w/replies, xml ) Need Help??

in reply to Re: Capturing both STDOUT, STDERR and exit status
in thread Capturing both STDOUT, STDERR and exit status

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.

Replies are listed 'Best First'.
Re^3: Capturing both STDOUT, STDERR and exit status
by polettix (Vicar) on May 06, 2005 at 22:34 UTC
    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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://454744]
[Corion]: choroba: Ouch! But you don't need that many external events for that. I have on several occasions set up milk to cook and then programmed a bit, only to find the milk burned and congealed over the stove :-/
[choroba]: it's still edible, just need to scour the pot
[1nickt]: Corion I have a large site I need to check for broken links and absolute links. Making a scraper is easy of course; a spider that crawls a whole site is a little more involved ... I was planning a queue-based tool. Intersted to see what you do...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (8)
As of 2017-10-18 11:44 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (244 votes). Check out past polls.