Interestingly, I'm not sure why method_1_2 not exported to the module2 namespace, but you can get around this by explicitly referring to the package:
module1.pm
package module1; use strict; use warnings; use module2 ();
module2.pm
package module2; use strict; use warnings; use module1 ();
Well, I think I see what is happening, but take it with salt. The use function effectively executes the BEGIN block and the BEGIN blocks are launched as soon as they are analyzed, so the code looks like this in the execution order.
perl starts parsing test.pl- he sees
use module1; , so it loads module1.pm and starts its parsing perl sees use module2; in module1.pm , so it loads module2.pm and starts parsing it- At this point, functions in
module1 do not yet exist, so they cannot be imported - parsing continues
Something Borodin said gave me the best solution: "@ module1 :: EXPORT doesn't even exist." The problem here is that the @EXPORT variable @EXPORT not exist. This can be fixed by putting it in the BEGIN block:
module1.pm
package module1; use strict; use warnings; use base 'Exporter'; BEGIN { our @EXPORT = qw( method_1_1 method_1_2 ); } use module2; sub method_1_1 { print "method_1_1\n"; module2::method_2_1(); } sub method_1_2 { print "method_1_2\n"; } 1;
module2.pm
package module2; use strict; use warnings; use base 'Exporter'; BEGIN { our @EXPORT = qw( method_2_1 method_2_2 ); } use module1; sub method_2_1 { print "method_2_1\n"; } sub method_2_2 { print "method_2_2\n"; method_1_2(); } 1;
IMPORTANT NOTE: I do not believe that prototypes in module1 will be executed in any of these cases (and I donβt how they can be since module2 compiled to module1 , so it cannot know that prototypes exist). This is another argument to never use prototypes .
source share