Is it possible in Perl to assign a new name to a module for use within our code?
My purpose is: some of my clients want .xls files (Spreadsheet::Excel) and others .xlsx (Excel::Writer::XLSX). Since both the modules share most of their API, I'd like to be able to set that option once at the beginning of the project some place and then forget about it, which would also make it easy to change it in the future. It could also perhaps used for things like Mouse/Moose changes.
It appears that all you really want is to be able to call class methods (like new
) on a class whose name is determined at runtime. That's actually quite simple:
my $spreadsheet_class = 'Spreadsheet::Excel';
my $sheet = $spreadsheet_class->new;
When you call a method on a scalar variable that contains a string, Perl treats it as a class method on the package of that name. No fancy symbol table hacks required, and it works just fine under use strict
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With