Features & Development

Problem with eval and $@

Re: Problem with eval and $@

by Davide Cervone -
Number of replies: 0
I did some testing on this, and it turns out that although
is trapped by the Safe compartment
eval {...};
is not. So this does work in the sense that it will trap errors. But it also turns out that $@ is not set by this, as you point out. I suspect that it may be that $@ is being set outside the Safe compartment, or something like that, but in any event, it is not being set within the PG problem. It might be the error trapping that Mike mentioned is interfering with it, but I haven't tried to look into that.

I'm surprised that eval {...}; is allowed, as Im pretty sure that wasn't the case in earlier versions of Perl or Safe.pm. I know that I had all kinds of heartache over this when writing the MathObjects library (so much so that I had to add Parser::Eval(sub {...}) to do essentially what eval {...} does. So I think something has changed along the way.

I don't know if anything can be done to fix that, but Parser::Eval() could probably be modified to set $@ to the error, if there is one.