package A; package B; use base 'Exporter'; use base 'Exporter'; our @EXPORT = qw(call_a); our @EXPORT = qw(call_b); use B; use A; sub call_a { sub call_b { return if (caller eq 'B'); call_a(); call_b(); } } 1; 1;
test.pl:
#!/usr/bin/perl use strict; use warnings; use lib "."; use A; call_a();
[erwan@mcbk ~/code]$ ./test.pl Undefined subroutine &B::call_a called at B.pm line 7.
(perl 5.8.6 on osx 10.4.9)
package A; package B; use base 'Exporter'; use base 'Exporter'; our @EXPORT = qw(call_a); our @EXPORT = qw(call_b); use B; use A; sub call_a { sub call_b { return if (caller eq 'B'); call_a(); # error! call_b(); } } 1; 1;
Let's try using fully qualified names
package A; package B; use B; use A; sub call_a { sub call_b { return if (caller eq 'B'); A::call_a(); B::call_b(); } } 1; 1;
#!/usr/bin/perl use strict; use warnings; use lib "."; use A; A::call_a();
[erwan@mcbk ~/code]$ ./test.pl [erwan@mcbk ~/code]$
Let's try delaying using A...
package A; package B; use base 'Exporter'; use base 'Exporter'; our @EXPORT = qw(call_a); our @EXPORT = qw(call_b); use B; sub call_b { require "use A"; sub call_a { require "import A"; return if (caller eq 'B'); call_a(); call_b(); } } 1; 1;
[erwan@mcbk ~/code]$ ./test.pl [erwan@mcbk ~/code]$
package B; use base 'Exporter'; our @EXPORT = qw(call_b); use later A; sub call_b { call_a(); } 1;
[erwan@mcbk ~/code]$ ./test.pl [erwan@mcbk ~/code]$