3

I am aware of cyclic module dependency in perl and the fact that it is very bad idea e.g.:

package ModuleA;
use ModuleB;

package ModuleB;
use ModuleA;

I want to ask if following model is safe and if it follows some best practicing rules:

package main;
use ModuleA;
use ModuleB;

package ModuleA;
use ModuleB;
use ModuleC;

package ModuleB;
use ModuleC;

Also I would like to ask if the order of use-ing modules have any impact? e.g. if

package main;
use ModuleA;
use ModuleB;

is the same as

package main;
use ModuleB;
use ModuleA;

and if

package ModuleA;
use ModuleB;
use ModuleC;

is the same as

package ModuleA;
use ModuleC;
use ModuleB;

etc.

EDIT: Note to say that ModuleA loads ModuleC explicitly (and do not rely on ModuleB that it will load ModuleC) because ModuleA uses functions from ModuleC. Is this good design approach?

7
  • Not enough information. Are these OO modules, functional modules, pragmatic modules? Are they based on Exporter? Commented Jan 20, 2015 at 13:22
  • Yes, they are using Exporter. Each module contains procedural code, I think. Can you please explain why that matters? Commented Jan 20, 2015 at 13:31
  • Because use = require + import. OO modules usually have no import, non-Exporter modules might have a wild import. Commented Jan 20, 2015 at 13:44
  • may I ask what do you mean by "wild import". Also when you have dependency as I mentioned (and also satisfied condition that needed modules are loaded explicitly e.g. ModuleA loads ModuleC explicitly and do not rely on ModuleB that it will load ModuleC) is it OK or is it some signal that the script should be redesigned to object oriented or functional or something? Commented Jan 26, 2015 at 16:44
  • "wild" import can do anything: create random subs in the caller's namespace, set global variables, install signal handlers, override core functions, etc. Commented Jan 26, 2015 at 21:38

1 Answer 1

2

The best practice is easy: Each file, program or module, should specify all its dependencies. That's it. E.g., if a script needs modules A and B, and module A needs module B, don't count on module B already loaded by the script - what if some other script needs module A without needing B?

Good Exporter based modules should use @EXPORT_OK and you should explicitly list the imported subroutines in the use-clause. It helps to prevent name clashes.

For normal modules that only export subroutines, order shouldn't matter. In other cases, it might, though: consider

use warnings_;
use diagnostics;

versus

use diagnostics;
use warnings_;
Sign up to request clarification or add additional context in comments.

5 Comments

Order does matter, depending on the modules, especially for modules that import subroutines. The last one wins when there is a conflict.
@choroba "if a script needs modules A and B, and module A needs module B" "what if some other script needs module A without needing B?" Isn't those two statements mutually exclusive? When I have Module A where is explicitly said that it use Module B (A cannot work without B) how is possible that somebody wants to use Module A but not Module B? In another words Module B is explicitly loaded when Module A is loaded and it cannot be changed, or can be? What do you mean by using @EXPORT_OK? If I understand you correct you say that Module A should contain in @EXPORT_OK subroutines from Module B?
@WakanTanka: By "use", I mean use, i.e. script C contains use A;, but doesn't contain use B;, because it's A's responsibility to handle its dependencies.
So basically you are saying that all modules which are needed should be loaded explicitly and do not relly on fact that they will be loased by another module which use them, am i right? Is this cobsidered as best practice? Thanks
@WakanTanka: Exactly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.