Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Eval and system()

by monkfish (Pilgrim)
on Sep 05, 2001 at 17:31 UTC ( [id://110286]=note: print w/replies, xml ) Need Help??


in reply to Eval and system()

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

Log In?
Username:
Password:

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

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

    No recent polls found