I working with a bunch of packages that shares the same interface. I need to execute the same subroutine on different class names and want to make it dynamic, i.e. something like in this example:
#!/usr/bin/env perl
use Modern::Perl;
use Data::Dumper;
use Mod1;
use Mod2;
my $mod = $ARGV[0];
my $meth = $ARGV[1];
${mod}::some_sub;
${mod}::${meth};
I need to call exactly subroutine and not a class' method. How I can achieve this? When I'm executing script above from CLI with arguments 'Mod1 some_sub' I'm getting script execution error with next message:
Bad name after :: at ./test.pl line 13.
or
Bareword found where operator expected at ./test.pl line 12, near "${mod}::some_sub"
(Missing operator before ::some_sub?)
syntax error at ./test.pl line 12, near "${mod}::some_sub"
Execution of ./test.pl aborted due to compilation errors.
for last 2 lines
Mod1.pm looks like this:
package Mod1;
use Modern::Perl;
use Data::Dumper;
sub some_sub {
say Dumper(\@_);
say 'in some_meth';
}
1;
And Mod2.pm code is next:
package Mod2;
use Modern::Perl;
use Data::Dumper;
sub other_meth {
say Dumper(\@_);
say 'other';
}
1;