Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Cyclic module references

by Heidegger (Hermit)
on Feb 07, 2003 at 16:52 UTC ( [id://233524]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Perl Monks,

I have two modules that reference each other. I get the following error when I want to use them: Can't locate object method "new" via package "DocumentManager" (perhaps you forgot to load "DocumentManager"?

Here's how my packages look like:

package DocumentManager; ... use Mokslas::EmployeesView; ...
package EmployeesView; ... use Mokslas::DocumentManager; ...

Well, maybe it's not a good design practice, but I wonder how could I make it work in Perl. Java wouldn't moan about it. In C++ I would workaround with forward declarations. How people resolve cyclic module dependency in Perl? Thank you very much.

Replies are listed 'Best First'.
Re: Cyclic module references
by bronto (Priest) on Feb 07, 2003 at 17:27 UTC

    Is package DocumentManager; a typo or real code? In case it is real code, I think it should be package Mokslas::DocumentManager;. The same holds for the other package declaration.

    Without further code, that's all I can say

    Ciao!
    --bronto


    The very nature of Perl to be like natural language--inconsistant and full of dwim and special cases--makes it impossible to know it all without simply memorizing the documentation (which is not complete or totally correct anyway).
    --John M. Dlugosz
Re: Cyclic module references
by BrowserUk (Patriarch) on Feb 07, 2003 at 17:10 UTC

    It's difficult to tell without sight of the source, but would it be possible to factor out the inter-dependancy into a third module and use it in each of the other two so breaking the cyclic dependancy?


    Examine what is said, not who speaks.

    The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.

Re: Cyclic module references
by PodMaster (Abbot) on Feb 07, 2003 at 17:31 UTC
    Do the package names match the file names?

    What is Monkslas::EmployeesView, and does it use Employeesview?

    Do what BrowserUk says, factor out the stuff you need, cause you cannot say

    #foo.pm package foo; use bar; #bar.pm package bar; use foo; #file.pl use foo;
    And expect it to work.

    Do the refactoring, it will help.

    You might wanna also investigate, the do, and use functions, and the %INC hash.


    MJD says you can't just make shit up and expect the computer to know what you mean, retardo!
    ** The Third rule of perl club is a statement of fact: pod is sexy.

Re: Cyclic module references
by blokhead (Monsignor) on Feb 07, 2003 at 18:46 UTC
    I don't think the problem is the cyclic references -- I think the problem is a typo between DocumentManager and Mokslas::DocumentManager, as bronto already suggested. Cyclic use statements are no problem for Perl:
    file mod1.pm: package mod1; use mod2; sub import { print "mod1 imported from " . (caller)[1] . "\n" } 1; file mod2.pm: package mod2; use mod1; sub import { print "mod2 imported from " . (caller)[1] . "\n" } 1; file use.pl: #!/usr/bin/perl use lib '.'; use mod1; use mod2; output: $ perl use.pl mod2 imported from mod1.pm mod1 imported from use.pl mod2 imported from use.pl $
    require and use will only load the file if it's not already loaded in %INC. Notice how mod2 never imports mod1.

    blokhead

Re: Cyclic module references
by BronzeWing (Monk) on Feb 07, 2003 at 17:58 UTC

    Greetings,
    Depending on how complicated your particular modules are, pieces of code you didn't represent here may need adjustment. Depending on what's there though, replacing use with require (followed by import where necessary) should provide a big step toward what you want. Consider for example, these modules which call functions from each other when they're loaded:

    #test.pl #!/usr/bin/perl use warnings; use strict; use Foo; print "test.pl completed.\n"; #Foo.pm package Foo; require Bar; &Bar::print_bar; sub print_foo { print "Foo!\n"; } 1; #Bar.pm package Bar; require Foo; &Foo::print_foo; sub print_bar { print "Bar!\n"; } 1;

    - BronzeWing

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-25 20:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found