views:

98

answers:

3

So I have a file that in short has this problem...

#!/usr/bin/perl -w
package Foo;

use strict;
use POSIX;

...

sub remove {
  ...
}
...

and I get a get an error saying the subroutine remove has been redefined. I know the problem, there is a subroutine called remove in POSIX. However, I don't know how to handle it. How is this problem typically solved?

+9  A: 

do this:

use POSIX ();

which will stop the export all default functionality of the POSIX module. You will then need to prefix all POSIX methods with POSIX:: such as:

POSIX::remove(filename)

for the POSIX remove function.

ennuikiller
Thanks. Is this conventional?
floogads
Yes, in general to avoid conflicting subroutine definitions its best to import nothing or only those subroutines you are sure will have unique names.
ennuikiller
@floogads, it's more usual to say `use POSIX qw(mkfifo modf);` where you explicitly list all the functions you want to import. That way, you don't have to use the `POSIX::` prefix all over, and you don't have to worry about conflicts with subroutine names in your program.
cjm
+2  A: 

The other way to suppress this warning is to put your subroutine redefinition inside a no warnings 'redefine' block:

{
    no warnings 'redefine';
    sub remove { ... }
}
mobrule
+7  A: 

You can exclude certain symbols from being normally exported with the '!name' directive (see perldoc Exporter), e.g.:

#!/usr/bin/perl -w
package Foo;

use strict;
use POSIX '!remove';

...

sub remove {
  ...
}
...
Ether
I didn't know about the '!name' directive! Neat!
Jim Davis

related questions