Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3287092
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:27:15+00:00 2026-05-17T20:27:15+00:00

As long as I can remember, whenever I use a module I include a

  • 0

As long as I can remember, whenever I use a module I include a use line at the beginning of my code.

Recently I was writing two Moose object modules that use each other. Look at this over-simplistic example:

One module:

package M1 0.001;

use Moose;
use 5.010;
use namespace::autoclean;
# use M2; ### SEE QUESTION BELOW

has 'name' => (
 is       => 'ro',
 isa      => 'Str',
 required => 1,
);

has 'very_cool_name' => (
 is      => 'ro',
 lazy    => 1,
 builder => '_build_very_cool_name',
);

sub _build_very_cool_name {
 my ($self) = @_;
 my $m2 = M2->new( m1 => $self );
 return 'very ' . $m2->cool_name();
}

__PACKAGE__->meta->make_immutable;

1;

Another module:
package M2 0.001;

use Moose;
use 5.010;
use Data::Dumper;    # TODO DEBUG REMOVE
use namespace::autoclean;
use M1;

has 'm1' => (
 is       => 'ro',
 isa      => 'M1',
 required => 1,
);

sub cool_name {
 my ($self) = @_;
 return 'cool ' . $self->m1->name();
}

__PACKAGE__->meta->make_immutable;

1;

And a short example that uses them:

use strict;
use warnings;
use 5.010;
use M1;
use M2;

my $m1 = M1->new(name => 'dave');
say $m1->very_cool_name();

Now, note the two modules use each other. M1 creates an instance of M2 and use it to generate very_cool_name, while M2 has an instance of M1 as an attribute.

Now, if I uncomment use M2; in M1 my eclipse goes mad. I guess it’s because this a the loop created by this ‘circular use`.

I commented this use and everything seems to works fine (I think…), but makes me really anxious (I’m using an object without use-ing its class! Is that ‘legal’?..). This also which made me wonder:

  • When do I really need to use use? I think that I was taught to always use it, and surely when I use an object.

  • Is there anything fundamentally wrong when two modules use each other (in the sense that each uses an object of the other module; I know there some cases where this is logically impossible, but sometimes – as in this case – I think it does make sense).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T20:27:16+00:00Added an answer on May 17, 2026 at 8:27 pm

    There’s no reason for M2 to use M1. You don’t actually have a recursive dependency.

    All M2 does is validate some object’s classname — which doesn’t require having loaded M1 — and call a method on it — which means whoever constructed that object loaded M1.

    Your rule about needing to use a class in order to call methods on an object of that class is wrong. use a class when you’re going to call methods on the it directly — for example, new. (This assumes pure OO modules, obviously, not things that export functions/symbols.)

    Consider polymorphism. It is a feature that I can make my own subclass of M1 (called, say, M1A) and pass it to M2 without M2 having to know anything about the existence of M1A.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For as long as I can remember I have avoided using switch statement fall-through.
So I've been doing this for as long as I can remember, but I'm
How long can I expect a client/server TCP connection to last in the wild?
The number is bigger than int & long but can be accomodated in Decimal
How can I split long commands over multiple lines in a batch file?
How can I cast long to HWND (C++ visual studio 8)? Long lWindowHandler; HWND
I can create a literal long by appending an L to the value; why
I can get simple examples to work fine as long as there's no master
I can find lots of information on how Long Polling works (For example, this
How can I transform a time value into YYYY-MM-DD format in Java? long lastmodified

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.