Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

capture errors from eval

by sdetweil (Sexton)
on Sep 23, 2012 at 20:50 UTC ( #995250=perlquestion: print w/ replies, xml ) Need Help??
sdetweil has asked for the wisdom of the Perl Monks concerning the following question:

I'm back again..

in prior topics I discussed a section of my code that allows my users to execute an arbitrary regular expression using

eval $w;

where $w contains the expression..

but, if they code it wrong, my application dies.

for example, expression= '/HOST=.*('

will yield a fatal die error..

Unmatched ( in regex; marked by <-- HERE in

but I don't want it to be fatal..

the contents of $w is (no quotes)
'$expdata =~ /HOST=.*(;'

but eval {$w;} is ignored

--- from the perl content on eval
eval '$x'; # CASE 3
eval { $x }; # CASE 4
Cases 3 and 4 likewise behave in the same way: they run the code '$x', which does nothing but return the value of $x. ---

and I can confirm that the expression itself does not execute.

So.. is there some trick to this?

thanks

Comment on capture errors from eval
Re: capture errors from eval
by tobyink (Abbot) on Sep 23, 2012 at 21:05 UTC

    Maybe...?

    eval { eval $w }
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      sorry, dumb answer.. only tested in one of two places.. this works great and $@ contains the error text..
      sorry, actually this does work!.. had two places and only tested on one (the wrong one obviously!).. thank you
Re: capture errors from eval
by BrowserUk (Pope) on Sep 23, 2012 at 21:28 UTC

    Does this work for you?:

    C:\test>perl -E"eval{ $SIG{__DIE__}='ignore'; eval $ARGV[0]; } say 'go +t here';" "$expdata =~ /HOST=.*(;" got here

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    RIP Neil Armstrong

Re: capture errors from eval
by ikegami (Pope) on Sep 24, 2012 at 01:13 UTC

    for example, expression= '/HOST=.*(' will yield a fatal die error..

    Not so.

    use strict; use warnings; my $w = '$expdata =~ /HOST=.*(;'; eval $w;

    eval catches fatals errors and places the message in $@.

Re: capture errors from eval
by tinita (Parson) on Sep 24, 2012 at 09:34 UTC
    I'd do it like this:
    my $re = eval { qr/$x/ }; if ($@) { ... } else { $string =~ $re }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://995250]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (14)
As of 2014-09-30 19:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (381 votes), past polls