You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Perrin Harkins <pe...@elem.com> on 2002/01/20 08:34:54 UTC

Re: handling eval in ePerl

> Umm I didnt mean to offend anyone in my previous posting - I did say I
> probably hadnt presented my situation properly.

No problem, I just meant "don't give up so quickly."

> Ofcourse you noticed I wrote ePerl/EmbPerl/Mason ?? I clubbed them
> together since I assume among other things you can embed perl code in
> HTML using either of them.

You can, but they don't share any code with ePerl.

> My problem is that die works fine as such but it conks out if done
> inside a eval.

Okay, I missed the part about eval() before.  Take a look at this code
from Parse::ePerl::Evaluate() :
local $SIG{'__DIE__'}  = sub { $error .= $_[0]; };

That's going to kill your exception handling code.  You need to change
that if you want to be able to use eval() in your code.  Matt has an
explanation of this in the exceptions part of the mod_perl Guide.

> It feels like being told to
> change gcc's code if my C code is not working :) - yah both of them
are
> written in C .

Apache::ePerl is written in perl.  It calls Parse::ePerl to do the dirty
work, and some of that is written in C, but not the part that's causing
you problems.

- Perrin


Re: handling eval in ePerl

Posted by Perrin Harkins <pe...@elem.com>.
> print STDERR "blah blah blah" is going to the browser but I am not
> really worried about it too much unless it is something I should worry
> about - anyone care to comment on that ?

Printing error messages to the public is a potential security risk, so
you have to decide how paranoid you want to be.  You could change this
behavior by modifying the tied STDERR in Parse::ePerl or maybe in
Apache::ePerl where it prints the message to the browser.

> Ofcourse I still dont understand why die was being trapped out there.

It is being trapped to allow for error reporting and to avoid leaving
the program in an unknown state when a problem occurs.  If you want it
to still work for this but not pick up your eval() calls, you can
replace the __DIE__ handler there with something fancier like this:

local $SIG{__DIE__} = sub {
     my $in_eval = 0;
     for( my $stack = 1;  my $sub = (CORE::caller($stack))[3];
 $stack++ ) {
         $in_eval = 1 if $sub =~ /^\(eval\)/;
     }
     $error .= $_[0] unless $in_eval;
};

This is a slight variation of some Michael Schwern code that Stas posted
a little while ago.

- Perrin



Re: handling eval in ePerl

Posted by Mithun Bhattacharya <mi...@egurucool.com>.
Perrin Harkins wrote:

> local $SIG{'__DIE__'}  = sub { $error .= $_[0]; };
> 
> That's going to kill your exception handling code.  You need to change
> that if you want to be able to use eval() in your code.  Matt has an
> explanation of this in the exceptions part of the mod_perl Guide.


Hmm,

I think just commenting it worked !!. I have tried the following which 
have worked the way it should.

-------------------
# Exception handling
eval {die "blah blah blah";};
print "got : $@";
-------------------
die "blah blah blah";
-------------------
# Write to error log
use Apache::Log;
Apache->request->log->error("got : $@");
-------------------
# Syntax error
eval {die "blah blah blah";
print "got : $@";
-------------------


print STDERR "blah blah blah" is going to the browser but I am not 
really worried about it too much unless it is something I should worry 
about - anyone care to comment on that ?

Ofcourse I still dont understand why die was being trapped out there.

Thanks to Perrin and Matt for their suggestions anyway.



Mithun