Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Nested Modules in perl

by gjoshi (Sexton)
on Aug 03, 2015 at 10:41 UTC ( [id://1137232]=perlquestion: print w/replies, xml ) Need Help??

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

Hi all,

I have couple of perl modules like connectivity.pm, ipc.pm, ue.pm, db.pm, general.pm

All these modules have functions according to their functionality.

Now there are certain modules call each other like connectivity.pm calls db.pm and db.pm calls connectivity.pm functions.

Now I can't call use db in connectivity and use connectivity in db.pm. It gives me error predefined and if I don't call then i am getting function missing error.

Could you please tell me how to achieve this kind of scenario?

thanks --girija

Replies are listed 'Best First'.
Re: Nested Modules in perl
by MidLifeXis (Monsignor) on Aug 03, 2015 at 12:16 UTC

    I believe that you are asking the question "If I have packages A and B, and A calls stuff from B (say, B::x()) and B calls stuff from A (say A::y()), how can I break up the circular dependency?"

    If this is a correct restatement of your question, create module C containing C::x() and C::y(), and have A and B require C, calling C::x() and C::y() instead.

    Often, when I see this crop up in my code, I have tried to include something in a module that does not belong that should be factored out into a separate module.

    --MidLifeXis

Re: Nested Modules in perl
by Laurent_R (Canon) on Aug 03, 2015 at 13:46 UTC
    Hi gjoshi,

    I have Mod2.pm:

    #!/usr/bin/perl -w package Mod2; use strict; use warnings; use Mod3; require Exporter; our $VERSION = '0.1000'; our @ISA = qw/Exporter/; our @EXPORT_OK = qw (double sixtimes); sub double { return 2 *shift} sub sixtimes { my $val = double shift; return Mod3::triple($val); }
    And Mod3.pm:
    #!/usr/bin/perl -w package Mod3; use strict; use warnings; use Mod2; require Exporter; our $VERSION = '0.1000'; our @ISA = qw/Exporter/; our @EXPORT_OK = qw (triple fourtimes); sub triple { return 3 * shift} sub fourtimes { my $val = Mod2::double(shift); return Mod2::double($val); }
    And my user program (usemod.pl) is as follows:
    #!/usr/bin/perl use feature qw /say/; use strict; use warnings; use Mod2 qw /sixtimes/; use Mod3 qw /triple fourtimes/; for my $x (0..4) { say "$x * 3 = ", triple($x); say "$x * 6 = ", sixtimes($x); say "$x * 4 = ", fourtimes($x); say "$x * 6 * 4 = ", sixtimes (fourtimes($x)); }
    This seems to work fine:

    Or did I miss something in your question?

Re: Nested Modules in perl
by Monk::Thomas (Friar) on Aug 03, 2015 at 11:41 UTC

    Please show code. Strip down some of your modules to the minimum functionality required to trigger the error and post the code.

Re: Nested Modules in perl
by vinoth.ree (Monsignor) on Aug 03, 2015 at 11:48 UTC
      Hi all, as MidLifeXis was saying I am trying to do same thing. Here is my code: Module Connectivity.pm
      use MyDB; our @ISA = qw(Exporter); our @EXPORT = qw(ConnectSsh SendAndGet_Cmd); sub ConnectSsh { #connect to shell #writes to DB WriteValueToDB ($text) } sub SendAndGet_Cmd { my ($cmd) = @_; #sends commands over shell handle } 1;
      MyDB.pm
      our @ISA = qw(Exporter); our @EXPORT = qw(EndRub WriteValueToDB ); sub EndRub { #send close command $op = SendAndGet_Cmd($test); #add status to DB WriteValueToDB ($op); } sub WriteValueToDB { my ($test) = @_; #write to DB ... .... .... } 1;
      Now you see both are dependent on each other. So if I am getting error declaration is already done.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2024-04-23 12:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found