Subject: | Avoid eval "require $object_class"; use eval { require $object_class_file } instead |
It would be nice if Class::Factory would avoid using
eval "require $object_class";
to load modules. I'm sure I don't have to tell you that eval EXPR is
much less efficient compared to eval BLOCK. It's especially good to
avoid it in a mod_perl environment, I think, where the code might be
executed many times.
I recommend the following code instead:
(my $object_class_file = $object_class) =~ s-::|'-/-g;
$object_class_file .= ".pm" if ($object_class_file !~ /\.p[lmh]$/);
eval { require $object_class_file };
See http://www.perlmonks.org/?node_id=634438 for related discussion. Tip
of the hat to PerlMonk tye for posting code similar to the above in that
thread. (It correctly deals with modules with names like Acme::Don't and
D'oh.)
The Class::Factory code indicates that the original author intended to
support $object_class values that end in ".p[lmh]", i.e. file names, I
presume. I'm not sure how useful that is in practice, but I've attempted
to retain that support with the "if ($object_class_file !~ /\.p[lmh]$/)"
conditional shown above.
If you want to be conservative, you could use the "eval BLOCK" form only
if ($object_class =~ /^\w+(?:::\w+)*$/) and use "eval EXPR" in all other
cases. I suspect that would cover 98% of Class::Factory's usage.
What do you think?