Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Giving alternate name to a module in Perl

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.

like image 620
Sundar R Avatar asked Dec 16 '22 06:12

Sundar R


1 Answers

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.

like image 114
cjm Avatar answered Jan 06 '23 12:01

cjm