Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi! I'd like to run a system command (eg. host -v www.perlmonks.org) and get the output saved in a variable (for parsing etc.) AND the return code to test wether the command succeeded. This is how I do it to get the output in a variable:
my $command = "host -v www.perlmonks.org 2>&1 |"; my $buff; if ( open( CMD, $command ) ) { while (<CMD>) { $buff .= "$_"; } close(CMD); } # do some parsing with $buff etc. # print $buff print "Output:\n$buff";
But this code doesn't give me the return code. To get only the return code I would use the following code:
my $RC = system("host","-v","www.perlmonks.org"); $RC = $RC/256; print "RC:\n$RC";
Now what I need is a combined version to get the return code and the output of the system command. Thanks for any advice

Replies are listed 'Best First'.
•Re: Return code and output of system command?
by merlyn (Sage) on Apr 01, 2003 at 23:05 UTC
      This is exactly what I was looking for, thanks!
Re: Return code and output of system command?
by grantm (Parson) on Apr 01, 2003 at 21:50 UTC

    Could you not just use backticks and get the exit status from $?

Re: Return code and output of system command?
by Pardus (Pilgrim) on Apr 02, 2003 at 00:05 UTC
    update: ooops didn't read the question good enough

    Use Shell, this module comes with perl5's basic package and does this by means of an autoloader routine.
    use Shell; my @list = ls(qw/-al/); # guess what @list is :)

    --
    Jaap Karssenberg || Pardus (Larus)? <pardus@cpan.org>
    >>>> Zoidberg: So many memories, so many strange fluids gushing out of patients' bodies.... <<<<