use parent 'Exporter' vs. use Exporter 

2019-07-07 12:30发布

问题:

What do I gain if I use the first example instead of the second example?

package Some::Module;
use strict;
use 5.10.1;

use parent 'Exporter';
our @EXPORT_OK = qw(some_func);

package Some::Module;
use strict;
use 5.10.0;

use Exporter 'import';
our @EXPORT_OK = qw(some_func);

回答1:

Nothing, really. Exporter was originally envisioned to work as a base class; later it was enhanced to allow you to just import its import method. There is no reason to inherit from it now.

If you are writing code that may be used with Perl 5.8.2 and earlier, you should require a sufficient version of Exporter:

use Exporter 5.57 'import';


回答2:

use Exporter 'import' works in Exporter 5.57+, which has been bundled with Perl since Perl 5.8.3. If you need to support the older versions of Exporter which were bundled with earlier releases of Perl, then you should inherit from Exporter instead.

(Exporter is a dual-life module, so users of older versions of Perl can technically upgrade Exporter without upgrading all of Perl; however older versions of the cpan tool sometimes have issues upgrading dual-life modules, so upgrading Exporter may be potentially tricky for them.)

If you don't care about archaic releases of Perl, by all means use Exporter 'import'. Exporter isn't really written in an object-/class-oriented fashion, so there's no real reason to inherit from it. (Unlike my own exporter module, Exporter::Tiny which is designed with inheritance/overriding in mind!)