As it happens, merlyn posted something similar the other day, which you could adapt easily (untested)...
# in Astro::Observation sub new { my $self = shift; # Foo->new('bar',@_) => Foo::Bar->new(@_) my $class = __PACKAGE__ . "::" . ucfirst(shift); require autouse; autouse->import( $class, "$class\::new" ); return $class->new( @_ ); }
Update:
... or not.
After trying out the above, I kept getting the error "autouse into different package attempted", which appears to be a known bug in autouse when using a module that isn't at the root of it's namespace (ie, Foo would work, but Foo::Bar doesn't).
Perhaps merlyn has a patched version of autouse?
Anyhoo, another way to do it (and as jeffa suggests) is with eval...
sub new { my $self = shift; # Foo->new('bar',@_) => Foo::Bar->new(@_) my $class = __PACKAGE__ . "::" . ucfirst(shift); eval "require $class;"; # Note: "" *NOT* {} ! if ( $@ ) { # whoops, there was an error warn( $@ ); # require'ing $class; perhaps return; # it doesn't exist? } return $class->new( @_ ); }
--k.
In reply to Re: Runtime instantiation decisions
by Kanji
in thread Runtime instantiation decisions
by LogicalChaos
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |