Why can't I call my exported subroutine in my Perl program?
I am new to Perl and I face following issue, having no clue why following is not workin开发者_如何学Cg.
My Perl module contains:
package PACK2;
use Exporter;
@ISA = ('Exporter');
@EXPORT_OK=('whom');
sub why(){
print "why\n";
}
sub whom(){
print "whom\n";
}
1;
My Perl file contains:
#!/usr/bin/perl -w
use pack;
use pack2 ('whom');
PACK::who();
&whom();
I run this program and can't find whom
:
perl use_pack_pm.pl
who
Undefined subroutine &main::whom called at use_pack_pm.pl line 7.
Perl is a case-sensitive language. I don't think modules "pack2" and "PACK2" are the same. (But I haven't actually tested this.)
Internally use pack2 ('whom');
is translated to something like
BEGIN {
require pack2;
pack2->import('whom');
}
Except that perl will check to see if it can call import
on pack2
before it tries to call it.
In your example there is no package named pack2
and so no import
function to call.
If your package name and file name match then perl would find the import
function provided by Exporter
.
There is no warning for this because Perl has a hard time telling when this was done deliberately.
Most OO modules won't export any functions or variables and so they don't privide an import
function.
Got same error using module from subfolder tree without declaring full path in package.
You should write package statement with its path.
For module located in subdirectory Dir
write package Dir::Module;
, not package Module ;
. Then it works.
精彩评论