开发者

How to handle subroutine redefined errors in Perl

开发者 https://www.devze.com 2023-01-13 19:04 出处:网络
So I have a file that in short has this problem... #!/usr/bin/perl -w package Foo; use strict; use POSIX;

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 redefi开发者_JS百科ned. 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?


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

{
    no warnings 'redefine';
    sub remove { ... }
}


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.


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 {
  ...
}
...


Subroutine XYZ redefined at line N [perl error]

You could wrap your routine in no warnings 'redefine' (which is lexically scoped):

no warnings 'redefine';

sub XYZ{

...
...

}

It worked for me!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号