Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
First things first (OK this is actually the last thing first):

system("ls") or die $!;

This is dieing because system returns: "The return value is the exit status of the program as returned by the wait() call." In this case the system is returning 0 to perl on success. This is one of the problems with dependind on response code from things outside of perl. The results aren't always perl friendly.

What you want is this:

# Works $return = system("ls"); if ($return != 0) { die ($_); } #Dies $return = system("no_such_command"); if ($return != 0) { die ($_); }

Now for the prior example:

eval { system("fake_command) }; print "$@";

I think you had a typo, but maybe you were trying to be really clever. Assuming it was a typo and you meant to include the close quote...

$@ is "The Perl syntax error message from the last eval() command. If null, the last eval() parsed and executed correctly (although the operations you invoked may have failed in the normal fashion)."

So the problem is that the perl code was fine, so there is nothing to report in $@. It is the system call that failed.

Now, if you were being really clever and trying to show us bad perl code (the missing quote) and asking why eval didn't trap that, it is because the code won't compile as written and creates a syntax error rather than a trappable run time error.

-monkfish (the fishy monk)

Editted for clarity. 9/5/01


In reply to Re: Eval and system() by monkfish
in thread Eval and system() by camelman

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (1)
As of 2024-04-25 04:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found