Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Exporter and subroutine circular dependencies between modules

0 views
Skip to first unread message

David Christensen

unread,
Mar 13, 2022, 10:45:03 PM3/13/22
to module-...@perl.org
On 3/13/22 16:12, Shawn H Corey wrote:

> Each module has its own runtime. That is, a module is loaded, compiled
> and run; all before the main program. In other words, when the module
> returns from its `use` statement, it has already run.


AIUI that is the view from 20,000 ft.. But, there are details and
interactions I do not perceive -- 'compile time' vs. 'run time', 'use'
vs. 'require' and 'import', the symbol table, recursion, etc.. I need
to dig into the Camel book again; I would rather avoid a code walk.


David

David Christensen

unread,
Jun 5, 2022, 10:15:07 PM6/5/22
to module-...@perl.org, sanfran...@pm.org
On 3/13/22 13:13, David Christensen wrote:
> I have been wrestling with the Exporter module and subroutine circular
> dependencies between modules for a number of years.

https://www.mail-archive.com/module-...@perl.org/msg10914.html


We revisited this topic at a San Francisco Perl Mongers meeting today:

https://mail.pm.org/pipermail/sanfrancisco-pm/2022-June/004851.html


I showed the following snippet of code that demonstrates the solution I
am currently using:

6 package Dpchrist::Lib5::Test;
7
8
9 use strict;
10 use warnings;
11 use threads;
12 use threads::shared;
13
14 our @EXPORT_OK;
15
16 BEGIN {
17 @EXPORT_OK = qw(
18 is_poly
19 );
20 }
21
22 use parent qw(
23 Exporter
24 Test::Builder::Module
25 );


The key points are:

* Put the Exporter-related statements (lines 14-25) near the top of the
module, before other code.

* Statement ordering is important:

* First -- declare @EXPORT_OK, but do not define/ initialize it (line
14).

* Next -- define/ initialize @EXPORT_OK in a BEGIN block (lines 16-20).

* Finally -- 'use parent' to inherit from Exporter (lines 22, 23, and
25).

* The above module also happens to inherit from Test::Builder::Module.
My other modules do not need or have line 24.

* As I develop code and introduce bugs, I frequently see warnings to the
effect "subroutine redefined" when there is a circular loop between
modules. Once I fix the bugs, those warnings go away.


Without understanding the "how" and "why" of perl(1), Exporter, "compile
time", "run time", "require", "use", "parent", "import", etc. -- of the
several solutions myself and others have tried over time, this one seems
to work the best for me.


David
0 new messages