Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Mini-Tutorial: Mutual Use of Exporting Modules

by tilly (Archbishop)
on Jul 10, 2009 at 04:30 UTC ( [id://778779]=note: print w/replies, xml ) Need Help??


in reply to Mini-Tutorial: Mutual Use of Exporting Modules

You shouldn't need to solve this problem.

If you do, the best solution should be to make one module use the other, and the other one require the first inside of the function(s) that need it. This gives a predictable loading order. For an example look at the source to Carp and Exporter.

If each really does need stuff from the other, and you're unwilling to use/require, then I would still suggest the cleaner pattern:

use Exporter qw(import); BEGIN {@EXPORT_OK = qw( ... );}
That makes the magic BEGIN block much shorter. And it avoids inheriting all of Exporter.

Also you need to warn people that it is very important that neither module in its initialization can do anything that relies on the other module being fully loaded yet. Because if the other module is used first, then its compilation will be put on hold to load this one, and therefore while this module gets loaded, that one will not be loaded. I've seen people get badly bitten by this, and get in a situation where one module breaks if the other is loaded first, or worse yet where neither works if the other is loaded first. (And that is one reason that I recommend the use/require relationship instead of use/use.)

If this scares you, then I'd suggest thinking through your problem clearly enough to not need to do circular dependencies. Many other languages don't even allow them, and I don't think that is a particularly bad restriction.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://778779]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2025-03-17 07:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When you first encountered Perl, which feature amazed you the most?










    Results (54 votes). Check out past polls.