in reply to Using Moo.pm and compilation errors

eval swallows all errors. This is not what you want if you suspect compile-time errors in your code. Instead, use require, especially when using OO-style modules:

... my $class="MHE::" . $opts->{type}; my $filename= $class) =~ s!::!/!g; require "$filename.pm"; ...

Alternatively, you can check $@ after the eval for errors:

eval "use MHE::$opts->{type} qw(sendHeartbeat); 1" or die "Couldn't load 'MHE::$opts->{type}': $@";

Replies are listed 'Best First'.
Re^2: Using Moo.pm and compilation errors
by Anonymous Monk on Apr 30, 2015 at 10:41 UTC

    require "$filename.pm";

    :) Module::Load is core long time :)

    $ perl -e"use Module::Load; load q/longtime/; " Can't locate longtime.pm in @INC (@INC contains:...

      Great! I didn't know that module. I reused the code that parent.pm uses to load classes. The sad thing is that parent still supports 5.8.x and Module::Load came into (core) existence in 5.9.x and I'm somewhat reluctant to ditch one regex for another use line in parent.pm. But for all my other module-loading needs, Module::Load is a welcome find!

Re^2: Using Moo.pm and compilation errors
by Anonymous Monk on Apr 30, 2015 at 10:33 UTC
    Thanks! That works brilliantly. I was assuming the error was in Moo, rather than the eval.

      Just for clarity, the eval or did not work, so I used the following code:

      eval "use MHE::$opts->{type} qw(sendHeartbeat)"; die "Couldn't load 'MHE::$opts->{type}': $@" if $@;
        What do you mean by "didn't work"? Have you added the 1 at the end of the eval?
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ