Reputation: 23501
I'm trying to avoid Module::Pluggable
and make thing easier.
Here's what I do, I have a set of modules stored in modules/
, all packages defined inside share the same method names:
# in XX.pm
package MyClass::XX;
sub new {}
sub method_1 {}
sub method_2 {}
# in YY.pm
package MyClass::YY;
sub new {}
sub method_1 {}
sub method_2 {}
(But note that there might be more than one module defined in a single .pm file)
Now I want to get all objects blessed to these modules, e.g [ XX->new, YY->new]
Is that possible?
Upvotes: 0
Views: 45
Reputation: 13664
You should be able to loop through this modules/
directory you say you have, and gather a list of files in it. (Use opendir
or better yet, Path::Tiny to gather the list of files.) Then:
my @classes = map {
(my $class = $_) =~ s{/}{::}g;
$class =~ s{\.pm\z}{};
$class;
} @files;
In the above I'm assuming a fairly standard filename-to-classname mapping. Feel free to tweak it if it doesn't fit your situation.
Once you have a list of classes, you can get back a list of objects pretty easily:
my @objects = map $_->new, @classes;
Personally I'd use Module::Pluggable though. Don't be afraid of the deprecation warnings. The module is not deprecated. What the warnings mean is that the copy of Module::Pluggable that comes bundled with the Perl interpreter is being phased out. Instead you should install a copy from the CPAN.
Upvotes: 2