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>

In reply to Re: system command by tachyon-II
in thread system command by palette

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.