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
In reply to Loading Modules At Run-Time With Interpolation by Cody Pendant
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |