Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: Using eval: $@ isn't returning the error I expect

by pryrt (Prior)
on Feb 19, 2020 at 23:05 UTC ( #11113200=note: print w/replies, xml ) Need Help??

in reply to [SOLVED] Using eval: $@ isn't returning the error I expect

This might help:

$ perl -le 'eval { "use gobbledygook;" }; print "ok"' ok $ perl -le 'eval { use gobbledygook; }; print "ok"' Can't locate in @INC ... $ perl -le 'eval "use gobbledygook;"; print "msg = ($@)" if $@; print + "ok"' msg = (Can't locate in @INC ...) ok

What you were doing was evaluating the code (the braces make it a code block) that contains a non-empty string; the non "0", non-empty string evaluates true, and the eval block doesn't error, so $@ doesn't get set. What you need to do is either evaluate block of real code in braces, or evaluate a string, not evaluate a string inside a block of code. The second and third in my example both try to use the module; the first (equivalent to your code) does not try to load the module.

Replies are listed 'Best First'.
Re^2: Using eval: $@ isn't returning the error I expect
by doctormelodious (Acolyte) on Feb 19, 2020 at 23:51 UTC
    Thanks, pryrt

    I tried this:

    $ perl -le 'eval "use cPanelUserConfig;"; print "msg = ($@)" if $@; print "ok"'

    and got this error:

    syntax error at /Library/WebServer/CGI-Executables/orders/orderFormSalesDisplayPreRollout.cgi line 13, near "$ perl "

    I don't think I'm correctly translating what you typed, into a literal line for my script. Can you tell me what I'm doing wrong? Thanks again.

      The $ perl -le ... indicates that this is a command typed at the command line (as a demo), not something you should simply copy & paste into your script as a whole line - the code is inside the single quotes after the -e switch here (see also perlrun).

        D'oh! Of course, that was a command line command... And yes, I did try just the part in single quotes in my script. Finally figured out via another comment here that it needed to go into a BEGIN block, due to the nature of its use on the host server. Problem solved. Thanks!

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2020-05-31 13:39 GMT
Find Nodes?
    Voting Booth?
    If programming languages were movie genres, Perl would be:

    Results (173 votes). Check out past polls.