ThorInSuburbia
ThorInSuburbia

Reputation: 63

Exported Perl sub not found

I run across this problem every once in a while. For some reason I get the "Undefined subroutine" error when trying to call an exported sub, and I have no idea why since it seems to happen out of the blue, in mature code.

The last time it happened I think I resorted to using something like "package_2::exported_sub()." That worked this time, but it simply returned an error for another sub in package_2. Even putting "use package_2;" in the line above doesn't help! The only thing I can think of is that the exported sub is being undefined somehow.

My code looks a little like this:

in file package_1.pm:

package package_1;

require Exporter;
@ISA = qw( Exporter );

@EXPORT = qw(
local_sub
);

use package_2;
use strict;
use warnings;
use diagnostics;

sub local_sub {
  &exported_sub;
}

in file package_2.pm:

package package_2;

require Exporter;
@ISA = qw( Exporter );

@EXPORT = qw(
exported_sub
);

use strict;
use warnings;
use diagnostics;

sub exported_sub {
 # do something
}

I'm at my wit's end...I was working on an unrelated hot ticket when this popped up, and user testing starts tomorrow!

Thanks in advance!


Update:

ikegami, thanks for your fix! I'm curious, though. I ran across this again, but this time I never did find the circular dependency. I narrowed it down to one line:

$row->{$attr} = ' ' unless ( $row->{$attr} );

Obviously, the line has nothing to do with using or requiring at all! I've looked in the Apache logs but nothing really seems to stand out, but will work to resolve whatever I find. I'll also see if I can get more warnings.

Aside from that, what do you recommend as a next step?

Thanks!

Upvotes: 1

Views: 1797

Answers (3)

PokerFace
PokerFace

Reputation: 801

I hope my experience can fit your case. Perl package name must fill with the Namespace. If your module file is in path $PERL_LIB/Fruit/Apple.pm Then in the module file, you must specify the Namespace Fruit::

package Fruit::Apple;

.......

If you only write

package Apple;

.......

there is no compile error. But when you call your method in module file Apple.pm, will get 'Undefined subroutine' error.

Upvotes: 0

ikegami
ikegami

Reputation: 385506

I suspect you have a circular dependency: package_1 uses package_2 and package_2 uses package1 (directly or indirectly).

What follows is a solution I originally published on PerlMonks: http://www.perlmonks.org/?node_id=778639


[ The need to use this technique is a very strong indicator of a design flaw in your system, but I recognize that the resources are not always available to fix design flaws. ]

If ModA uses ModB, ModB uses ModA, and ModA or ModB imports symbols from the other, one needs to pay attention to code execution order. The best way I've found to avoid problems is to setup Exporter before loading any other module.

ModA.pm:

package ModA;

use strict;
use warnings;

BEGIN {
   our @ISA = qw( Exporter );
   our @EXPORT_OK = qw( ... );
   require Exporter;
}

use This;
use ModB;
use That;

...

1;

ModB.pm:

package ModB;

use strict;
use warnings;

BEGIN {
   our @ISA = qw( Exporter );
   our @EXPORT_OK = qw( ... );
   require Exporter;
}

use This;
use ModA;
use That;

...

1;

Upvotes: 4

DVK
DVK

Reputation: 129363

Are you sure your use line is exactly like that an not something like use package_2 qw(xyz); ?

The latter would define what Exporter's import() subroutine would export into the caller module.

Upvotes: 1

Related Questions