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

I have cmd to be executed.I am using back ticks (``) to execute them. But I am not able to capture the return error message if the command fails.
print "Start of my code\n"; my @array = `cmd to be executed`; my $exit_status = $?; print "Exit status is : $exit_status\n"; if ( $exit_status != 0) { print "Error in executing command : $! \n"; exit; }
Output is
Start of my code Expired or invalid session token Exit status is : 13312 Error in executing command :
Desired output is
Start of my code Exit status is : 13312 Error in executing command : Expired or invalid session token

Replies are listed 'Best First'.
Re: Capture return value from a cmd when it errors
by Athanasius (Archbishop) on Aug 05, 2014 at 13:09 UTC

    Backticks (or qx//) capture output to STDOUT, but not to STDERR. For an easy solution, see Capture::Tiny.

    Update: For example (adapted from the documentation):

    use Capture::Tiny ':all'; my $cmd = ... my @args = ... print "Start of my code\n"; my ($stdout, $stderr, $exit) = capture { system($cmd, @args); }; print "Exit status is : $exit\n"; if ($exit) { print "Error in executing command : $stderr\n"; exit; } print $stdout;

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: Capture return value from a cmd when it errors
by mr_mischief (Monsignor) on Aug 05, 2014 at 14:13 UTC

    If you're using bash or a shell sufficiently similar to bash then you can redirect STDERR to STDOUT.

    my @array = `cmd to be executed 2>&1`;

    The advice Athanasius gave about Capture::Tiny is good advice, though. It should be much more portable to do it that way.

Re: Capture return value from a cmd when it errors
by Anonymous Monk on Aug 05, 2014 at 14:18 UTC

    My recommendations: Use system from IPC::System::Simple as a replacement for system with better error handling, use capture from that module as a replacement for backticks with better error handling, or, if you want to also capture the STDERR of an external command, use IPC::Run3.

    Capture::Tiny (as recommended by Athanasius) is also a very nice module, but it leaves the execution of the external command and handling of errors up to the user.

Re: Capture return value from a cmd when it errors
by roboticus (Chancellor) on Aug 05, 2014 at 13:24 UTC

    sravs448:

    Are you sure you're not getting the exit status? The value you show (13312) translates to 0x34 after you shift it right by 8 bits, which looks like a reasonable return value. However, I don't know what value you expected to see there. (You need to shift it right 8 bits to get the actual returned value, as mentioned in perldoc perlvar)

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      I am looking for return message not the code .sory if i am not clear in my question.
Re: Capture return value from a cmd when it errors
by Lotus1 (Vicar) on Aug 05, 2014 at 13:17 UTC

    Have a look at ${^CHILD_ERROR_NATIVE} in this Q&A node. How do I get both the return value and text? backticks vs. system().

    Update: You'll notice the status value from backticks is returned to $? but perl adds a byte on the end so you need to shift it 8 bits to the right to get the code. That being said I've had problems with third party executables that had large return values. $? wasn't large enough to hold both the Perl added byte and the original 32 bit return code.

      I am looking for return message not the code .sory if i am not clear in my question