zerohero has asked for the wisdom of the Perl Monks concerning the following question:
I'm wondering if there is a better (safer, faster, etc.) way of dynamically loading a class by it's name, and then invoking the constructor (using the class name). The key is we don't want to have to say "use BAREWORD_CLASS", but want to get some of the same benefits. Here's what I'm doing now:
sub creator_func { my ($class, $arg1, $arg2) = @_; eval "require $class"; if ($@) { print "Load error for class='$class': $@\n"; return; } my $new_obj = $class->new ($arg1, $arg2); return $new_obj; }
Is this inefficient? Will it compile the perl class every time it hits the require statement?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Dynamic Class Loading, Compilation, & Speed
by zwon (Abbot) on Mar 06, 2010 at 22:56 UTC | |
|
Re: Dynamic Class Loading, Compilation, & Speed
by jdrago999 (Pilgrim) on Mar 07, 2010 at 02:21 UTC | |
by shmem (Chancellor) on Mar 07, 2010 at 19:40 UTC | |
|
Re: Dynamic Class Loading, Compilation, & Speed
by cdarke (Prior) on Mar 06, 2010 at 23:04 UTC | |
by zwon (Abbot) on Mar 06, 2010 at 23:14 UTC | |
|
Re: Dynamic Class Loading, Compilation, & Speed
by Anonymous Monk on Mar 06, 2010 at 23:21 UTC |