On 3/13/22 16:13, David Christensen wrote:
module-authors:

I have been wrestling with the Exporter module and subroutine circular dependencies between modules for a number of years.  I have yet to find a satisfactory solution.

[...]

What is the "proper" way to avoid or solve the problem of subroutine circular dependencies between modules?

The issue in your case is in part[1] due to the fact that in Foo11.pm, this part is run during the execution phase:

   require Exporter;
   our @ISA    = qw( Exporter );
   our @EXPORT    = qw( foo )

while

   use Bar11;

is actually run earlier, during the compilation phase, so Bar11 can't get any symbols from Foo11 because it's not actually exporting them yet.

If you move import of modules with circular dependencies to runtime rather than compile time:


   $ diff Exporter-circular-use{.orig,.new}
   diff Exporter-circular-use.orig/Bar11.pm
   Exporter-circular-use.new/Bar11.pm
   13c13,14
   < use Foo11;
   ---
    > require Foo11;
    > Foo11->import;
   diff Exporter-circular-use.orig/Foo11.pm
   Exporter-circular-use.new/Foo11.pm
   13c13,14
   < use Bar11;
   ---
    > require Bar11;
    > Bar11->import;


   $ perl Exporter-circular-use.t
   ok 1 - foo00
   ok 2 - foo01
   ok 3 - foo10
   ok 4 - foo11
   1..4

 Or move the export completely into compile time:

   $ diff Exporter-circular-use{.orig,.new}
   diff Exporter-circular-use.orig/Bar11.pm
   Exporter-circular-use.new/Bar11.pm
   9,11c9,11
   < require Exporter;
   < our @ISA    = qw( Exporter );
   < our @EXPORT    = qw( bar );
   ---
    > use parent 'Exporter';
    > our @EXPORT;
    > BEGIN{ @EXPORT = qw( bar ); }
   diff Exporter-circular-use.orig/Foo11.pm
   Exporter-circular-use.new/Foo11.pm
   9,11c9,11
   < require Exporter;
   < our @ISA    = qw( Exporter );
   < our @EXPORT    = qw( foo );
   ---
    > use parent 'Exporter';
    > our @EXPORT;
    > BEGIN { @EXPORT = qw( foo ); }

it also will work;


[1] I'm not completely solid on symbol resolution time in Perl, so I'll prevaricate.

Reply via email to