Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

system command

by palette (Scribe)
on Jun 18, 2008 at 12:16 UTC ( #692684=perlquestion: print w/replies, xml ) Need Help??

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

I have a .pl script. Inside the .pl script I am using a system command to call another script.

Consider the first script as and the second one as

There are print stmts in and there are chances that can throw error.

I have to catch the error in eval block and print the error in the script code snippet

... .. eval { system("perl"); }; if($@) { print "the error that was thrown in"; } ...

Pls note if is displaying any output that should not be treated as error and caught in $@;

Replies are listed 'Best First'.
Re: system command
by tachyon-II (Chaplain) on Jun 18, 2008 at 12:44 UTC

    You can't do this (well you can but it won't DWIM):

    eval { system("perl"); }; if($@) { print "the error that was thrown in"; } ...

    all it does is see if system("perl") is valid perl regardless of 1) whether the system call succeeds or fails and 2) what happens when actually runs. A system call may confuse you because if it *succeeds* it will return 0 (false) and if it fails it will return true! Thus you can't do:

    system("blah") or die "Can't blah $!\n";

    because this will die when it works! The reason is that in *nix programing if a program suceeds it exits with a value of 0. If it fails it exits with a numerical value that corresponds to the error code.

    In the real world, in you would have:

    my $ret_val = system("perl"); if ( $ret_val == 0 ) { print "Success"; } else { printf "Error. Err Number: %d", $ret_val<<8; }

    And in you would have:

    exit 0 if $success; exit $err_num if $err_num; # numerical error value exit -1; # WTF???

    Usually you would exit with the error number or exit 0 at the (sucessful) end of the script.

    open F, $file or do { warn "Can't open $file for read $!\n"; exit 1; # Error code for can't open file } do_stuff(*F); close F; exit 0;
      I cant ensure having a return stmt in

      There are many other scripts which has to be included in which are legacy and I cant modify that.

      But when executes and if it's successful usually a perl script should return a range of value.

      Let me know the range of values that will be returned when the script is executed successfully and the range when it's failure.

        1. So might return anything.
        2. You can't do anything about that
        3. On success returns WTF it feels like (see 1)
        4. And you want me to let *you* know the range of values that will be returned when the script is executed successfully and the range when it's failure

        Sorry. Computer says no. Does not compute.

        Maybe try backtics

        $output = `perl`; if ($output =~ m/some random error message/) { die "Computer says no"; }

        Alternatively look up do and wrap that in an eval. This is archaic but is probably what you want.

        eval{ do '' }; die "Aaaagh! $@" if $@;
Re: system command
by psini (Deacon) on Jun 18, 2008 at 12:28 UTC

    You could use IPC::Open3 or consider the possibility of transforming your into a module

    Careful with that hash Eugene.

Re: system command
by olus (Curate) on Jun 18, 2008 at 12:32 UTC
Re: system command
by zentara (Archbishop) on Jun 18, 2008 at 15:21 UTC

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://692684]
Approved by olus
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (1)
As of 2023-05-31 00:17 GMT
Find Nodes?
    Voting Booth?

    No recent polls found