http://www.perlmonks.org?node_id=700546

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

I'm trying to do syntax checks on my perl libraries using perl -c. However, I often get the following types of warnings:
Subroutine XXXX redefined at XXX.pm line YYY.

After some investigations, I believe this is because I am directly loading the file I am testing using the command line. This does not count towards the "use" count of the library. So when the file under test, uses another library, which in turn uses the file under test, the subroutines get loaded a second time.

Is there any way round this?
  • Comment on Circular dependencies when using the perl syntax checker

Replies are listed 'Best First'.
Re: Circular dependencies when using the perl syntax checker
by ikegami (Patriarch) on Jul 28, 2008 at 15:52 UTC
    Don't execute modules. The error you show will go away if you do
    perl -c -we"use modA"

    or

    perl -MmodA -cwe1

    Treating the module as a script doesn't set %INC, so modA.pm gets compiled and executed twice, once as a script and once as a module (the first time it's used).

Re: Circular dependencies when using the perl syntax checker
by moritz (Cardinal) on Jul 28, 2008 at 14:35 UTC
    I tried to reproduce your error:
    $ echo 'package A; sub foo { }; 1' > A.pm $ echo 'package B; use A; 1;' > B.pm $ perl -w -I. -MA -wle 'use B' # no warnings here

    I use A twice, once via the command line, and once indirectly by using B. The -w switch actives warnings globally, and still no warnings about redefinitions.

    Maybe you could show us some code that demonstrates your problem?

      I was thinking of something more like:

      echo 'package modA; use modB; sub foo { }; 1' > modA.pm
      echo 'package modB; use modA; 1;' > modB.pm
      perl -c -w modA.pm
      Subroutine foo redefined at modA.pm line 1.
      modA.pm syntax OK

      die $INC{'B.pm'};
      C:\>echo package B;sub VERSION{333};1; >B.pm C:\>more B.pm package B;sub VERSION{333};1; C:\>perl -MB -le"print for %INC;die B->VERSION" XSLoader.pm C:/Perl/lib/XSLoader.pm C:/Perl/site/lib/sitecustomize.pl C:/Perl/site/lib/sitecustomize.pl Exporter.pm C:/Perl/lib/Exporter.pm strict.pm C:/Perl/lib/strict.pm B.pm C:/Perl/lib/B.pm 1.09 at -e line 1.
Re: Circular dependencies when using the perl syntax checker
by alexm (Chaplain) on Jul 28, 2008 at 15:41 UTC
Re: Circular dependencies when using the perl syntax checker
by jettero (Monsignor) on Jul 28, 2008 at 14:44 UTC
    If you post some of the code, or equiv that produces the error(s), we'll come up with much better guesses. Possibly even a good explanation of what's happening (and why).

    -Paul

Re: Circular dependencies when using the perl syntax checker
by tsee (Curate) on Dec 01, 2008 at 15:08 UTC

    This is a bit of a late post, but I've just stumbled on this thread because I had the same problem. Other posts explained the cause of it, so I won't go into that. I'd just like to point out a workaround which seems to work for me: Say, we have module Foo which resides in its own file Foo.pm. You can put the following code right below the package statement.

    BEGIN { $INC{"Foo.pm"} ||= __FILE__ }

    Essentially, this forces an entry in %INC when the file is compiled. Use with care.

    Cheers,
    Steffen

      I had this specific problem with Class::MethodMaker as well. Perhaps because I had my files inside of subdirs (e.g. My::Module in My/Module.pm), Steffen's fix didn't work. I had to take another step, and make sure I had the filename just so:

      BEGIN { use strict; use File::Spec; $INC{"My/Module.pm"} ||= File::Spe +c->rel2abs(__FILE__); }

      (OK, the "use strict" is just a bit of belt-and-suspenders ;)

Re: Circular dependencies when using the perl syntax checker
by Anonymous Monk on Jul 28, 2008 at 14:37 UTC
    1. Don't do that (write better test code)
    2. Supress warnings (no warnings ... )
      2. Supress warnings (no warnings ... )

      As John Macenroe once famously said @ Wimbledon 'You cannot be serious' ... this should only be done by those who know what they're doing and with great care - don't look gift horses etc. etc. To ignore a problem doesn't fix it.

      useing strict & warnings are provided for a purpose!!

      Wonders why aren't they aren't both used by default - being disabled as required ???

      At last, a user level that overstates my experience :-))
        useing strict & warnings are provided for a purpose!!

        Yes, so is no warnings 'redefine';