rovingeyes has asked for the wisdom of the Perl Monks concerning the following question:

I am working on an internal Perl based client for a REST service. So I have two OO modules, let us say A & B. The module A is the one that the end user uses & the module A in turn "uses" module B, which is pretty much a wrapper around HTTP::Request module to make HTTP requests to the server. This could look like:
use A; my $a = A->new(); $a->execute(<params>);

Module A has an execute() method, which when invoked, creates the headers etc & sends data to the server using module B. Now this is where it gets interesting for me.

This REST service (apart from other stuff) returns a representation of a custom CSV file object. So, I can say get me a handle to a particular CSV file & it'll return a JSON representation of it. And on this, I can do other operations like data mining (which will be happening on the server). I want to represent this object as say C. So when I say $a->getCSVHandle(), I should be returning to the end user an object type C. That means module A will import module C and in getCSVHandle() method, it instantiates class C (with its relevant instance vars) & retunrs it. So, when I call any methods on this C object, it should do some relevant data munging & in turn use module A's execute method to send the HTTP request, because that is the end goal anyway.

But to use the module A's execute method, I will have to "use" it in module C, which will create a circular dependency as module A already imports C, which I want to avoid. How can I achieve the end result without getting in to this nasty circular dependency?

Replies are listed 'Best First'.
Re: Accessing a module's method without causing circular dependency
by ikegami (Patriarch) on Apr 27, 2010 at 19:48 UTC
    You shouldn't have any problem unless one of the modules requires one of the others at compile-time. e.g. If one of the module imports from one of the others in the cycle, you might have problems.

    But to use the module A's execute method, I will have to "use" it in module C

    Not true. Class A must have already been loaded if an object of class A was previously created.

      Oh, so I guess I misunderstood how the use statement itself works. I have to read the manual for it again. So does that mean when you say "use <some module>", if it is already loaded, Perl does not load it again? If that is the case then I guess I don't have to worry here. Thanks.
        So does that mean when you say "use <some module>", if it is already loaded, Perl does not load it again?

        Yes, Perl keeps track of loaded modules in %INC.

        Correct. The module is neither compiled nor executed a second time, although it's import method is called every time use is used (unless prevented using () or qw()).

        use: "The require makes sure the module is loaded into memory if it hasn't been yet."

Re: Accessing a module's method without causing circular dependency
by almut (Canon) on Apr 27, 2010 at 19:34 UTC

    Have you just tried it? What error do you get?

    In short, there's no problem with modules using one another (as per your description):

    package A; use C; sub new { return bless {}, $_[0]; } sub execute { print "A::execute()\n"; } sub getCSVHandle { my $c = C->new(); return $c; } 1; ----- package C; use A; sub new { return bless {}, $_[0]; } sub munge { my $a = A->new(); $a->execute(); } 1; ----- #!/usr/bin/perl use A; my $a = A->new(); my $c = $a->getCSVHandle(); $c->munge(); # prints "A::execute()"