views:

143

answers:

4

I have asked this question before or searched and seen others ask - why am I getting the warning "Subroutine mySub redefined at ../lib/Common.pm line x"? and you always get the answer you declared the sub twice in the same code. I created this test package:

ENTIRE FILE ---------------

package Common;

use strict;

sub thisSubroutineIsNotDefinedAnywhereElse{
}

1;

ENTIRE FILE ---------------

and I USE this package from a perl script, which uses other packages, that use this package also, and I get the warning:

Subroutine ThisSubroutineIsNotDefinedAnywhereElse redefined at ../lib/Common.pm line 19.

I promise I did not declare this sub anywhere else. So is this caused by a circular reference? How can I go about tracking the cause of this warning down and fixing?

+7  A: 

You can't return from a mainline. That should just be 1. Like so:

#...
sub thisSubroutineIsNotDefinedAnywhereElse{
}

1;

You may hear people occassionally speaking about how modules have to "return 1", but that's how you do it. Not return 1;.

Axeman
`return 1;` and `1;` is the same thing in Perl. You can also `sub x { 1; }` which is equal to `sub x { return 1; }` or `sub x { $return_this; }`
jmz
i agree with the comment - "1;" and "return 1;" are the same thing. also, unrelated to my question.
+2  A: 

If you're on a system with a case-insensitive filesystem (Windows, and quite often OSX), and you do use Common in one file and use common in another, you can cause problems like this.

hobbs
searched for occurrences of this and could not find any. very good to know though - thanks!
A: 

I tried to use "package Common.pm" as a package name. The compiler gave me errors. Very kind of it eh? What version of Perl are you using? I tried it on 5.10.0 and 5.12.1.

Even if you can compile it is good practice to remove the .pm file. For Example;

File: some_package.pm;

package some_package;
use strict;

sub yadayadayada { ... }

1;
J.J.
A: 

Do you have a dependency loop? If Perl starts compiling your script and encounters a line like this:

use PackageA;

Perl pauses the compilation of your script; locates PackageA.pm and starts compiling it. If it encounters a line like this:

use PackageB;

Perl pauses the compilation of PackageA; locates PackageB.pm and starts compiling it. Normally, that would complete successfully, and Perl would go back to complete compiling PackageA and when that completes successfully it would go back to compiling your script and when that completes successfully it would start to execute the compiled opcodes.

However, if PackageB.pm contains this line:

use PackageA;

You might expect it would do nothing since Perl has already processed PackageA.pm but the problem is that it hasn't finished yet. So Perl will pause the compilation of PackageB and start compiling PackageA.pm again from the beginning. That could trigger the message you're seeing about subroutines in PackageA being redefined.

As a general rule, two packages should not both depend on each other. Sometimes however the loop is harder to locate because it is caused by a third package.

Grant McLean
i think this is my problem - how to fix then? it's not a circular reference - i would see a circular reference as PackageA has "use PackageB;" and PackageB has "use PackageA;" Rather I have a script "main.pl" that uses PackageA and uses PackageB, and PackageA also uses PackageB. Shouldn't be anything wrong with that?
Correct, there should be nothing wrong with that.
Grant McLean