Multiple Perl Exporter Packages

I am trying to familiarize myself with the Perl exporter, the problem I am facing is what I am trying to use. I can not use the exporter with modules containing several packages. What am I missing below?

MyModule.pm use strict; use warnings; package Multipackage1; use Exporter; our @ISA = qw(Exporter); our @EXPORT = qw(test1); sub test1 { print "First package\n"; } 1; package Multipackage2; use Exporter; our @ISA = qw(Exporter); our @EXPORT = qw(test2); sub test2 { print "Second package\n"; } 1; package Multipackage3; use Exporter; our @ISA = qw(Exporter); our @EXPORT_OK = qw(test3); sub test3 { print "Third package\n"; } 1; MyMainFile.pl #!/usr/bin/perl use strict; use warnings; use Multipackage; use Multipackage qw(test3); print "Calling first package:\n"; test1(); print "Calling second package:\n"; test2(); print "Calling third package:\n"; test3(); 

I get test1, not included in the main package.

Thanks in advance.

+8
perl
source share
1 answer

use calls require , which looks for a file with the package name (c / for :: and + .pm ).

So require actual package file, then import from the packages.

main.pl

 use warnings; use strict; require MyModule; import Multipackage1; import Multipackage2; import Multipackage3 qw(test3); print "Calling first package:\n"; test1(); print "Calling second package:\n"; test2(); print "Calling third package:\n"; test3(); 

In MyModule.pm put each package in its own block to provide scope for lexical variables, since package does not. Since v5.14 (I think), you can do this with package Pack { ... } . For all of these 1 s there is no need, and you can pull out use Exporter; from blocks.

Exit

 Calling first package:
 First package
 Calling second package:
 Second package
 Calling third package:
 Third package

Better yet, replace our @ISA = qw(Exporter); on use Exporter qw(import); for

 use strict; use warnings; package Multipackage1 { use Exporter qw(import); our @EXPORT = qw(test1); sub test1 { print "First package\n" } } ... 1; 

with the same exit.

Please note that placing multiple packages in one file is usually not required and is not performed.

+8
source share

All Articles