Excel::Writer::XLSX for Perl: How do I test a feature is supported by the module?

202 Views Asked by At

I have exercised a lot of features of this module, but sometimes have to install into an environment that has an older version of the module that doesn't have the features I have implemented.

So what's the best way to handle this? - Version check? - try{} catch{}?

1

There are 1 best solutions below

1
Trenton Trama On

All perl objects extend from the UNIVERSAL module which

is the base class from which all blessed references inherit

You can call can on the module to test to see if a particular method is implemented in the package.

perl -MExcel::Writer::XLSX -e 'if (Excel::Writer::XLSX->can("set_vba_name")) {print "It can";}';

perl -MExcel::Writer::XLSX -e '$xlsx = Excel::Writer::XLSX->new("/tmp/file");  if ($xlsx->can("add_worksheet")) {print "It can"}';

I'd avoid using version checking, it makes testing and development much harder, but can work in a pinch.

Ultimately, you want a consistent runtime environment, so you could create a makefile that installs all of the dependencies at the version you need.