ThorInSuburbia ThorInSuburbia - 4 months ago 19
Perl Question

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!

Answer

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;