Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

system command

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

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

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

Consider the first script as 1.pl and the second one as 2.pl

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

I have to catch the error in eval block and print the error in the script 1.pl

1.pl code snippet

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


Pls note if 2.pl 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 2.pl"); }; if($@) { print "the error that was thrown in 2.pl"; } ...

    all it does is see if system("perl 2.pl") is valid perl regardless of 1) whether the system call succeeds or fails and 2) what happens when 2.pl 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 1.pl you would have:

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

    And in 2.pl 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;
    </code>
      Hi,
      I cant ensure having a return stmt in 2.pl.

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

      But when 2.pl 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 2.pl might return anything.
        2. You can't do anything about that
        3. On success 2.pl 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 2.pl`; 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 '2.pl' }; 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 2.pl 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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-04-20 01:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found