Cody Pendant has asked for the wisdom of the Perl Monks concerning the following question:
Package Foo has sub-packages Foo::Bar and Foo::Baz.
When I do
I load those modules:my $foo = Foo->new qw( Foo::Bar Foo::Baz );
as part of new {}.my @submodules = @_; for ( @submodules ) { eval "require $_" || die "$!"; }
But I can't figure out how to abbreviate the submodule names to just "Bar" and "Baz" and still have it work.
For instance I would like to do
and have new {} do this:my $foo = Foo->new qw( Bar Baz );
or similar. But when I do it that way, although it doesn't die, the submodule's methods don't seem to be available.for ( @submodules ) { eval "require Foo::$_" || die "$!"; }
This works:
package Foo; sub new { my $class = shift; my @submodules = @_; for ( @submodules ) { eval "require $_" || die "$@"; } bless { submodules => \@submodules }, $class; } 1;
package Foo::Bar; sub details { return { name => 'Simon', job => 'doctor', }; } 1;
#!/usr/bin/perl use Foo; my $foo = Foo->new qw(Foo::Bar Foo::Baz ); for ( @{ $foo->{'submodules'} } ) { print $_->details->{'name'} . " is a " . $_->details->{'job'} . $/; } # prints: Simon is a doctor
This doesn't work:
package Foo; sub new { my $class = shift; my @submodules = @_; for ( @submodules ) { eval "require Foo::$_" || die "$@"; } bless { submodules => \@submodules }, $class; } 1;
package Foo::Bar; sub details { return { name => 'Simon', job => 'doctor', }; } 1;
#!/usr/bin/perl use Foo; my $foo = Foo->new qw( Bar ); for ( @{ $foo->{'submodules'} } ) { print $_->details->{'name'} . " is a " . $_->details->{'job'} . $/; } # prints: Can't locate object method "details" via package "Bar" # (perhaps you forgot to load "Bar"?)
($_='kkvvttuu bbooppuuiiffss qqffssmm iibbddllffss')
=~y~b-v~a-z~s; print
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Loading Modules At Run-Time With Interpolation
by Tanktalus (Canon) on Aug 12, 2005 at 04:23 UTC | |
by Cody Pendant (Prior) on Aug 12, 2005 at 04:30 UTC | |
by Cody Pendant (Prior) on Aug 12, 2005 at 04:35 UTC | |
by Tanktalus (Canon) on Aug 12, 2005 at 04:47 UTC | |
by Cody Pendant (Prior) on Aug 12, 2005 at 05:01 UTC | |
|
Re: Loading Modules At Run-Time With Interpolation
by Tanktalus (Canon) on Aug 12, 2005 at 01:56 UTC | |
|
Re: Loading Modules At Run-Time With Interpolation
by tlm (Prior) on Aug 12, 2005 at 03:23 UTC | |
by Cody Pendant (Prior) on Aug 12, 2005 at 04:09 UTC |