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

Hello , I'm moving my scripts to packages, but there is an error I can't understand why it happens.
For example Gl/Df.pm:
package Gl::Df; use strict; use warnings; require Exporter; our @ISA = qw(Exporter); our @EXPORT = qw( tst1 ); our $VERSION = 1.00; use Gl::Uf; sub tst1 { tst2(); print 1 ;} 1;
Gl/Uf.pm:
package Gl::Uf; use strict; use warnings; require Exporter; our @ISA = qw(Exporter); our @EXPORT = qw( tst2 ); our $VERSION = 1.00; use Gl::Df; sub tst2 { tst1();print 2 } 1;
Writing a script that wants to use both modules and their functions test.pl
use strict; use warnings; use Gl::Df; use Gl::Uf; tst1(); tst2();
Error received :
Undefined subroutine &Gl::Uf::tst1 called at Gl/Uf.pm line 14.
What may be the problem ? tst1 should be exporter to 3 scopes, and it is when I checking in the %Gl::Df:: and %Gl::Uf:: but Perl can't use them
How I can fix that ?
Thanks a lot !

Replies are listed 'Best First'.
Re: Package problem with defining subroutines (circular dependence)
by Anonymous Monk on Feb 05, 2013 at 15:39 UTC
Re: Package problem with defining subroutines
by Anonymous Monk on Feb 05, 2013 at 15:48 UTC
    You've got infinite loop
      I know, but how I can prevent it by design ?
      For example you have DB.pm which all modules uses it .
      Then a User.pm and UserRating.pm , that using each other for retrieving info
      Reading posts from #1 reply (tnx)

        Refactor, break the circle

        sub tst1 { mutual_util(); ... } sub tst2 { mutual_util(); ... }

        Take whatever tst1 does that calls tst2, take whatever test2 does that calls tst1 , make it a new function, and instead of calling each other, call this third function

Re: Package problem with defining subroutines
by AlfaProject (Beadle) on Feb 05, 2013 at 16:30 UTC
    Thanks for help , I decided not to use packages. It's seems not applicable in practice with a lot of code and complex dependencies.
    Next time I just will use the OOP approach.

      Next time I just will use the OOP approach.

      But that uses packages too :p