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

Ok, I am so sick of banging my head against the wall on this one. I have read through previous answers to this question and none help me.

Setup: I have two modules A and B and both 'use' each other since they make references to each other. Now package B has 'use vars' since it has some global data.

I the "Subroutine xxxx redefined" and the sub isn't declared twice, but as soon I remove the 'use B' from the A package, errors go away.

I tried 'use subs' on the B subroutines to try and get rid fo the error with no luck. I don't import name space and always use fully qualified paths to the other package's subroutines.

Anyone got any ideas? How do I have global variables and have the subs use each other? I tried getting rid of 'use vars' but that did not go over well when I tried to use the variables outside of the package (fully qualified of course). The var $A::foo was empty when displayed in package B.

Replies are listed 'Best First'.
Re: Subroutine redefined
by GrandFather (Saint) on Aug 16, 2006 at 03:34 UTC

    Post some code. Read I know what I mean. Why don't you? first.

    You should be able to reproduce the problem in 20 lines of code or fewer in one file. By the time you have done that most likely you will have figured out the problem for yourself and you will have learned a good diagnostic technique.


    DWIM is Perl's answer to Gödel
Re: Subroutine redefined
by Anonymous Monk on Aug 16, 2006 at 03:35 UTC
    Ok, followup. The 'use vars' is a red herring.
    package TestPm; use strict; use warnings; use TestPm2; sub foo { print "foo\n"; } 1;
    and:
    package TestPm2; use strict; use warnings; use TestPm; 1;
    Doing a 'perl -c TestVar.pm' will spit out the Subroutine redifined error. I know its kinda circular thing, but I am really surprised perl can't handle this, so I am guessing that _I_ am not handling it correctly.

    Any help would be great.

      Perl won't run a module twice, but when running a .pm as a script, it doesn't count as module. In other words, your module is fine. It's how you run it that's broken.

      Replace
      perl -c TestVar.pm
      with
      perl -ce "use TestPm"
      to fix the problem.

      However, I think there's a design problem when two modules include each other.

        DOH!

        You know, I never actually ran it. I just do the perl -c compulisivly before I even run it. Sure enough:
        #!/usr/bin/perl use strict; use warnings; use TestPm; TestPm::foo();
        Works fine, no errors. Thanks a mil.

      Refactor your code so the common stuff both need is in one (probably a third) package or combine the packages if they are so intertwined that they can't survive appart from each other.


      DWIM is Perl's answer to Gödel
        I was thinking of that, but not sure. I am refactoring a codebase other than mine and I am going from one 10k line .pl file to varios libs, but I don't want to go crazy.

        Basically, I had a Web lib for displaying information to the user and a Storage lib for actually doing stuff like saving/getting data from persistant storage.

        I wanted to pass an errorHandler subroutine reference in to the Storage funtion, but what if one wasn't provided? I wanted to have a default one use, which was in the Web package.

        Its late, and I am sure you probably have better things to do, but thats my issue. I felt 3 packages excessive, but couldn't think of a cleaner way. Ultimately I made the Storage func return 0 if it had problems.