http://qs321.pair.com?node_id=110284


in reply to Eval and system()

It's not being caught in the compile phase. The program compiles just fine.

You are working just fine with eval ... it's system (specifically on Unix) that's tripping you up. Try doing:

eval { system "fake command" && print "Blah1\n" }; eval { system "fake command" || print "Blah2\n" }; eval { system "ls" && print "Blah3\n" }; eval { system "ls" || print "Blah4\n" };
Unix commands are built off of the C libraries (for the most part). The standard C libraries, for reasons which will be left to your imagination, use '0' to indicate success and non-zero to indicate failure. Thus, the Unix commands tend to follow this (rather stupid) paradigm.

In addition, system handles all the cleanup from failed commands. If you want to test $@, try

eval { die "Hello!\n" }; print "Reason: $@\n"; eval { die "Hello!" }; print "Reason: $@\n";
Try them both - they exhibit different behaviors of eval and die.

------
We are the carpenters and bricklayers of the Information Age.

Vote paco for President!

Replies are listed 'Best First'.
Re (tilly) 2: Eval and system()
by tilly (Archbishop) on Sep 07, 2001 at 18:29 UTC
    Not entirely true. For many programs if you get back $? of 256 (which is what a return code of 1 looks like), that is not necessarily an errror.

    Here is why. The return codes in the standard C libraries are an error code. 0 means, appropriately enough, no error. After that it is up to the author's imagination. In complex applications this generally means a standardized list of messages (kind of like the list enshrined in $!). In smaller applications people often use the convention that 1 is a minor issue that is not really an error and 2 is a fatal error.

    For instance try this:

    perl -e 'print system("ls no_such_file")'
    That gives back 256. It doesn't list anything, but none of its operations ran into trouble, and you might not have expected the file to be there. Now try that with a directory that you don't have permission to access. You will get back a more serious error - it cannot give you a real answer because you are not permitted to know that.
Re: Re: Eval and system()
by pmas (Hermit) on Sep 05, 2001 at 18:30 UTC
    • The standard C libraries, for reasons which will be left to your imagination, use '0' to indicate success and non-zero to indicate failure.
    I remember it this way: routine returns error number. If returns 0 => no errors. It makes sense for C, any kind of TRUE value means some error. But you can have only one kind of success, so OK == 0.

    If you looking at return value this way, it makes sense, right?

    pmas
    To make errors is human. But to make million errors per second, you need a computer.

Re: Re: Eval and system()
by camelman (Sexton) on Sep 05, 2001 at 19:36 UTC
    Thanks, monks. As always I learned quite a bit by asking a question here. I think I'll be going with:
    eval { system "fake command" && print "Blah1\n" };
    Initially I thought
    eval {system ("fake command")} or print "Blah1\n";
    was correct, but the first example works the way I want it to. Thanks . . . Kevin
Re: Re: Eval and system()
by busunsl (Vicar) on Sep 05, 2001 at 17:29 UTC
    Sorry to insist, but: eval { system("fake_command) }; does not compile on my machine, due to a missing " and that's correct behaviour.

    Perhaps your Perl has a bug?