in reply to Re^2: Finding out whether a module contains a certain package
in thread Finding out whether a module contains a certain package

If your program is really terminating, are you sure you are even finishing compilation? eval doesn't need one to throw an explicit exception or even a die statement in the enclosed code. The only thing it won't "catch" is an explicit exit statement buried somewhere in the enclosed code. Do you mean to say that Class::MixinFactory is calling exit? I searched Class::MixinFactory::Factory where the class(...) subroutine is defined and could find no such call.

Consider these two examples. The first terminates because of division by 0, but the second does not. Neither throw exceptions.

First example using a hard-coded division by 0 (compilation error before we even get to eval)

sub foo { 5/0; } print "Before foo\n"; eval { foo 0; return 1; } or do { print "Oops!\n" }; print "Going onto other things\n"; # output Illegal division by zero at ...

Second example using a variable denominator (runtime error)

sub foo { 5/$_[0]; } print "Before foo\n"; eval { foo 0; return 1; } or do { print "Oops!\n" }; print "Going onto other things\n"; #outputs Before foo Oops! Going onto other things

But even if eval { ... } doesn't work, you could still scan the symbol table for the packages involved in the mix-in after you call class(...) and then throw your own exception if you don't like what you see, i.e. if (scalar @aNotFound) { die ... }. I'm sure you've already thought of that, so perhaps you could explain why that doesn't work for you?

Best, beth

Update: clarification of timing of compilation error.

Replies are listed 'Best First'.
Re^4: Finding out whether a module contains a certain package
by rovf (Priest) on May 05, 2009 at 11:38 UTC

    Your example does not apply in my case, because in your case, the program would not even start to run. My problem applies to a program, which is already running (i.e. already produced output). When eval finds a syntax error during compile, eval says:

    If there is a syntax error ..., an undefined value is returned by "eval", and $@ is set to the error message.

    Incidentally, when rechecking my code, I indeed found a bug, in that I don't properly handle the case when eval returns undef, so maybe this explains what I have seen: eval *does* return, but returns undef, and my program crashes when it uses that undef value. I will fix this, to be on the safe side.

    -- 
    Ronald Fischer <ynnor@mm.st>