in reply to Runtime instantiation decisions
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.
|
---|
Replies are listed 'Best First'. | |
---|---|
•Re: Re: Runtime instantiation decisions
by merlyn (Sage) on Mar 30, 2002 at 18:29 UTC | |
by LogicalChaos (Beadle) on Mar 30, 2002 at 19:01 UTC |