in reply to loading modules using sub

You have circular inheritance - A uses B uses A uses B... Perl resolves this by importing a module only once. In your case, this means that depending on the order of processing, abc() may or may not be exported before def() is defined.

The solution is don't do this. Circular dependence can usually be replaced with simple inheritance with no loss of functionality. In this case, the 'solution' is to simply not have module A use B. More often, the easiest solution is abstracting the shared code into a common module 'C'.

#!/usr/bin/perl use lib "."; use A; use B; def(); ######################################## A.pm package A; require Exporter; @ISA = qw(Exporter); @EXPORT = qw(abc); use C; ######################################## B.pm package B; require Exporter; @ISA = qw(Exporter); @EXPORT = qw(def); use C; sub def{ print "Inside sub def of package B -> calling sub abc from package + C\n"; abc(); } ######################################## C.pm package C; require Exporter; @ISA = qw(Exporter); @EXPORT = qw(abc); sub abc{ print "Inside sub abc of package A\n"; }

Replies are listed 'Best First'.
Re^2: loading modules using sub
by Anonymous Monk on Jun 25, 2010 at 06:21 UTC
    Thanks kennethk, What i knew was 'use' puts the modules in compile-time, so i assumed how ever circular the 'use' call may be, at the point of execution, both the A.pm and B.pm are well resolved. Can you plese let me know how perl walks through the above script(abc.pl), and may fail in loading some functions. Wanted to have a clear picture of how perl compiles and executes.