如何处理 Perl 中的子程序重定义错误

发布于 2024-09-16 02:33:19 字数 267 浏览 9 评论 0原文

所以我有一个文件,简而言之,有这个问题...

#!/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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

万水千山粽是情ミ 2024-09-23 02:33:19

抑制此警告的另一种方法是将子例程重定义放入 no warnings 'redefine' 块中:

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

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

{
    no warnings 'redefine';
    sub remove { ... }
}
无所的.畏惧 2024-09-23 02:33:19

执行此操作:

use POSIX ();

这将停止导出 POSIX 模块的所有默认功能。然后,您需要为所有 POSIX 方法添加 POSIX:: 前缀,例如:

POSIX::remove(filename)

用于 POSIX 删除函数。

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.

非要怀念 2024-09-23 02:33:19

您可以使用 '!name' 指令排除某些符号的正常导出(请参阅 perldoc出口商),例如:

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

use strict;
use POSIX '!remove';

...

sub remove {
  ...
}
...

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 {
  ...
}
...
水溶 2024-09-23 02:33:19

在第 N 行重新定义子例程 XYZ [perl 错误]

您可以将例程包装在没有警告的“重新定义”(这是词法范围)中:

no warnings 'redefine';

sub XYZ{
  ...
  ...
}

它对我有用!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文