Re: Undefined subroutine errors
by ikegami (Patriarch) on Oct 26, 2005 at 00:01 UTC
|
Design issues asside, Exporter needs to be loaded (and its configuration variables must be initialized) before the other module of the pair is loaded. Using the following templates will allow both modules to import from each other:
use strict;
use warnings;
package SpiderMan;
BEGIN {
our @ISA = qw( Exporter ... );
our @EXPORT_OK = qw( ... );
require Exporter;
}
use ...;
use ...;
use Doctor::Octopus;
use ...;
use ...;
...
1;
use strict;
use warnings;
package Doctor::Octopus;
BEGIN {
our @ISA = qw( Exporter ... );
our @EXPORT_OK = qw( ... );
require Exporter;
}
use ...;
use ...;
use SpiderMan;
use ...;
use ...;
...
1;
| [reply] [d/l] [select] |
|
|
You were correct. It works now. Thanks. Can you or someone else please tell me why I need the BEGIN blocks?
| [reply] |
|
|
use Module
means
BEGIN {
require Module;
Module->import if Module->can('import');
}
| [reply] [d/l] [select] |
Re: Undefined subroutine errors
by shemp (Deacon) on Oct 25, 2005 at 23:15 UTC
|
It would be helpful to see exactly how these module are including one another. Are they being use'd or require'd? If the modules are packages, are the use or require statements within the packages, or outside of the package declarations?
Also, i may get flamed for this, but it seems like a design flaw to have two modules that depend on each other....flame away!
I use the most powerful debugger available: print!
| [reply] |
|
|
I'm sure no sensible person would flame you for suggesting that OP's base problem looks like a design issue. Given that OP seems to have control over all the code involved OP's best option may be to refactor the code to remove the circular dependencies that are implied, or at least move the circular code into a common module so that that nastyness doesn't leak out to pollute other code.
However, assuming that the modules are currently being "used", requiring may fix the issue. OP should take a look at Using a module more than once which has some interesting and pertinent replys.
Perl is Huffman encoded by design.
| [reply] |
|
|
Although uncommon, the intentions of OP might not
necessary be a design flaw.
His situation is similar to C code when we export symbols, not from
the library to main, but from plugin to other plugins using the
RTLD_GLOBAL with the 2nd parameter to dlopen(), so plugins can have
access to the symbols of one another.
OP has advanced an interesting problem. My efforts so far
reveal that the problem is trivially solved by calling the functions with
their full package names. It is the import mechanism of Export that fails
to make things more convenient and allow us to import all symbols to
both namespaces at once.
| [reply] |
|
|
| [reply] |
Re: Undefined subroutine errors
by leriksen (Curate) on Oct 26, 2005 at 00:57 UTC
|
Another solution - maybe not a good one - is this
#!/usr/bin/perl -w
use strict;
use warnings;
use Spiderman qw(shootWeb);
print STDERR shootWeb() . "\n";
package Spiderman;
use base qw(Exporter);
our @EXPORT = qw(shootWeb webStrength);
use DrOctopus ();
sub shootWeb {
return DrOctopus::deflectWeb(); # from DrOctopus
}
sub webStrength {
return 1;
}
package DrOctopus;
use base qw(Exporter);
our @EXPORT = qw(deflectWeb);
use Spiderman ();
sub deflectWeb {
return Spiderman::webStrength(); # from Spiderman
}
1;
...reality must take precedence over public relations, for nature cannot be fooled. - R P Feynmann
| [reply] [d/l] |
|
|
None of the functions provided by Exporter are called in your snippet. Why bother using Exporter?
Update: In answer to my question, I suppose it gives the Green Goblin and other third parties the ability to import from these modules, but it detracts from the point of using fully qualified function names.
| [reply] |
|
|
| [reply] |
|
|
Re: Undefined subroutine errors
by dragonchild (Archbishop) on Oct 26, 2005 at 00:33 UTC
|
Refactor the items that both need into a third module (Hulk?) and have both SpiderMan and DoctorOctopus 'use' it.
My criteria for good software:
- Does it work?
- Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
| [reply] |
Re: Undefined subroutine errors
by sauoq (Abbot) on Oct 26, 2005 at 02:33 UTC
|
(Hey, at least, I didn't use Mister Ed and the characters from Gilligan's Island for module names, okay?).
Alas, you missed your chance. You could've called the modules Conjunction.pm and Junction.pm...
-sauoq
"My two cents aren't worth a dime.";
| [reply] |
Re: Undefined subroutine errors
by EvanCarroll (Chaplain) on Oct 25, 2005 at 22:52 UTC
|
| [reply] |
|
|
What are you talking about? These aren't CPAN modules. These are my own modules that I wrote. The error appears whenever I use the modules (after both have been installed).
| [reply] |