develooper Front page | perl.module-authors | Postings from March 2022

Re: Exporter and subroutine circular dependencies between modules

Thread Previous | Thread Next
From:
Diab Jerius via module-authors
Date:
March 13, 2022 22:09
Subject:
Re: Exporter and subroutine circular dependencies between modules
Message ID:
a67a9dde-a2af-3842-1590-635a9509f7e9@cfa.harvard.edu
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.


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About