Take a look at the program package MyCommonPkg.pm
and see what it says. Does it have something like this?
package MyCommonPkg;
use Exporter qw(import); # Might be "require" and not "use"
our @EXPORT = qw(thisSubroutineIsNotDefinedAnywhereElse);
The syntax may be a bit different. The main things you should see are the package
statement, that it's using Exporter
and that the @EXPORT
array has your subroutine's name in it.
What is going on is a namespace clash. Your package is defining the same subroutine you're defining.
In order to prevent this from happening, Perl uses namespaces. By default, your namespace is main
. However, packages are suppose to define their own separate namesakes using the package
command.
The full namespace of a subroutine or variable is the namespace followed by a double colon, followed by the subroutine or variable name. For example, of you look at File::Find, you will see references to the variables $File::Find::name
and $File::Find::dir
. These are the variables $name
and $dir
inside the File/Find.pm
package under the File::Find
namespace.
In order to make things easier for you, packages can export their variables and subroutines into your main namespace. For example, if I use File::Copy, O can do this:
...
use File::Copy
...
copy ($file, $to_dir);
Instead of:
...
use File::Copy
...
File::Copy::copy ($file, $to_dir);
If you look at File/Copy.pm
, you will see the following:
package File::Copy;
...
our(@ISA, @EXPORT, @EXPORT_OK, $VERSION, $Too_Big, $Syscopy_is_copy);
...
require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(copy move);
The package File::Copy;
defines a namespace. The require Exporter;
and the @ISA = qw(Exporter)
allows the package to export subroutines and variables into the main namespace. The @EXPORT
automatically, without it telling you anything, imports the copy
and move
subroutines into the main namespace whether you want them or not!
That last bit is very important. It is now considered bad manners to use @EXPORT
. Instead, you should use @EXPORT_OK
which requires you to list the subroutines you want to use. More modern packages like Scalar::Util do this.
So several things. First, does your MyCommonPkg
have a package MyCommonPkg;
statement. If not, it should. This keeps the packages subroutines and variables from affecting your program in nasty ways. Then, you can use @EXPORT
or @EXPORT_OK
.
If MyCommonPkg
does have a package
statement, does it use @EXPORT
? If so, you have several ways you can avoid this issue:
- Ignore the warning. It's just a warning. Since you know you're redefining the subroutine, and you want to use your definition of the subroutine, ignore it.
You can do this to turn off the warning as you redefine the subroutine:
use MyCommonPkg;
no warnings qw(redefine);
sub thisSubroutineIsNotDefinedAnywhereElse {
...
}
use warnings qw(redefine);
- Use
require MyCommonPkg;
instead of use MyCommonPkg;
. This will prevent the importing of any subroutines or variables into your namespace including ones you wanted to use. Let's say MyCommonPkg
defines four subroutines: thisSubroutineIsNotDefinedAnywhereElse
, foo
, bar
, and barfoo
. To use any of these subroutines.
You need to do this:
my $answer = MyCommonPkg::foo( $input );
Not fun.
Use another name for your subroutine. It should have been documented that this subroutine is defined in MyCommonPkg
, and if you want to use MyCommonPkg
, you shouldn't use subroutine names that are exported.
Finally, if MyCommonPkg
is fairly new, and isn't used in dozens of programs, use @EXPORT_OK
instead of @EXPORT
, and make sure all the programs that use MyCommonPkg
are modified to export the subroutines they want:
Like this:
use MyCommonPkg qw(foo bar);
In this case, only subroutines foo
and bar
are exported. The subroutines thisSubroutineIsNotDefinedAnywhereElse
and barfoo
are not exported into your environment.
package Common.pm
? That seems like an error. – Ardel/
with::
, and stripping.pm
). This can also happen if you have no namespace, which really means that you are inmain
. – Joby