in reply to Subroutine redefined

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.

Replies are listed 'Best First'.
Re^2: Subroutine redefined
by ikegami (Patriarch) on Aug 16, 2006 at 03:42 UTC

    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.
Re^2: Subroutine redefined
by GrandFather (Saint) on Aug 16, 2006 at 03:39 UTC

    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.