如何处理 Perl 中的子程序重定义错误
所以我有一个文件,简而言之,有这个问题...
#!/usr/bin/perl -w
package Foo;
use strict;
use POSIX;
...
sub remove {
...
}
...
并且我收到一条错误消息,指出子例程 remove
已被重新定义。我知道问题所在,POSIX 中有一个名为 remove
的子例程。但是,我不知道如何处理。这个问题通常是如何解决的?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
抑制此警告的另一种方法是将子例程重定义放入
no warnings 'redefine'
块中:The other way to suppress this warning is to put your subroutine redefinition inside a
no warnings 'redefine'
block:执行此操作:
这将停止导出 POSIX 模块的所有默认功能。然后,您需要为所有 POSIX 方法添加 POSIX:: 前缀,例如:
用于 POSIX 删除函数。
do this:
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:
for the POSIX remove function.
您可以使用 '!name' 指令排除某些符号的正常导出(请参阅 perldoc出口商),例如:
You can exclude certain symbols from being normally exported with the '!name' directive (see perldoc Exporter), e.g.:
在第 N 行重新定义子例程 XYZ [perl 错误]
您可以将例程包装在没有警告的“重新定义”(这是词法范围)中:
它对我有用!
Subroutine XYZ redefined at line N [perl error]
You could wrap your routine in no warnings 'redefine' (which is lexically scoped):
It worked for me!