Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: system command

by tachyon-II (Chaplain)
on Jun 18, 2008 at 12:44 UTC ( [id://692693]=note: print w/replies, xml ) Need Help??


in reply to system command

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>

Replies are listed 'Best First'.
Re^2: system command
by palette (Scribe) on Jun 18, 2008 at 12:58 UTC
    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 $@;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2024-04-19 05:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found