Joe
Joe

Reputation: 401

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 working.

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.

Upvotes: 3

Views: 4493

Answers (3)

doublebind
doublebind

Reputation: 11

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.

Upvotes: 0

Ven'Tatsu
Ven'Tatsu

Reputation: 3625

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.

Upvotes: 5

reinierpost
reinierpost

Reputation: 8591

Perl is a case-sensitive language. I don't think modules "pack2" and "PACK2" are the same. (But I haven't actually tested this.)

Upvotes: 8

Related Questions