Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
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

Replies are listed 'Best First'.
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 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 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, actually this does work!.. had two places and only tested on one (the wrong one obviously!).. thank you
      sorry, dumb answer.. only tested in one of two places.. this works great and $@ contains the error text..
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?
[Corion]: ambrus: Yeah - we're in that situation too, except that there is no time to do the reorganizing :-/
[LanX]: ... so my boss started a project with the newest sun servers and invited the traders to come on weekend to test it... and they were so pleased, that they forced him to keep it in production...
[ambrus]: Corion: sure, this is the long-term plan. The short term is that I have to run this ungodly mess to get results from the new input data today.
[Corion]: ambrus: Most of our "automation" is tied to process exit codes and a shell pipeline :-\
[LanX]: ... a week later they realized that one of the databases - which recorded how much the other banks due to this bank - was not correctly plugged
[ambrus]: Corion: I have no problem with exit codes and shell pipeline. My problem is that the current process requires a lot of manual intervention from me, including editing the source codes.
[ambrus]: (Also a lot of manual intervention by two or three other co-workers, who do other parts of the process.)
[ambrus]: Some of the manual part is unavoidable, but not all.
[choroba]: LanX was there a way to recover the numbers from the remaining information?
[Corion]: LanX: Ow ;)

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (12)
As of 2017-03-29 11:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (350 votes). Check out past polls.