2 个不同 CPAN 模块中具有相同名称的子例程

Mar*_*rgo 6 perl module subroutine

运行 perl 时出现此错误:

[Thu Mar 16 00:24:23 2023] list_directory_1.cgi:子例程 main::getcwd 在 /usr/lib/cgi-bin/list_directory_1.cgi 第 15 行重新定义。

我认为这是来自getcwdCPAN 模块CwdPOSIX. 如何指定从模块中获取该子例程Cwd

ike*_*ami 10

事实上,Cwd 和 POSIX 都getcwd默认导出。

$ perl -we'use Cwd; use POSIX;'
Subroutine main::getcwd redefined at -e line 1.
Run Code Online (Sandbox Code Playgroud)

解决方案是仅导入您需要的符号。

use Cwd      qw( abs_path );
use POSIX    qw( strftime floor );
use DateTime qw( );                  # Import nothing.
Run Code Online (Sandbox Code Playgroud)

如果您始终采用这种明确列出导入的方式,您将获得能够一目了然地看到潜艇来源的好处。