Perl readdir 警告 - 名称 "main::DIR"仅使用一次 : possible typo

标签 perl

我有一个脚本列出了特定目录中可能的文件。代码工作正常,但如何避免这个警告?

#!/usr/bin/perl

use strict;
use warnings;
use autodie;

my $logpath = "C:/Users/Vinod/Perl/Log";

opendir(DIR, $logpath);
while (my $file = readdir(DIR)) {

    next unless (-f "$logpath/$file");
    print "FILENAME:$file\n";

}
closedir(DIR);

编译或运行脚本时显示的警告是:

$ perl -cw log_fetch.pl
Name "main::DIR" used only once: possible typo at log_fetch.pl line ...
log_fetch.pl syntax OK

最佳答案

这似乎是使用 use autodie; 的一个奇怪的副作用。

可以按如下方式消除警告:

sub x { *DIR }  # Silence spurious "used only once" warning.

但是,您不必要地使用了全局变量 (*DIR)。使用词法变量会好得多,但这会解决问题。

opendir(my $DIR, $logpath);

关于Perl readdir 警告 - 名称 "main::DIR"仅使用一次 : possible typo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61822681/

相关文章:

perl - DBD::mysql::st fetchrow_array 失败:fetch() 没有execute()

Perl 测试 : How to stop testing in a single file after first failure

perl - split() 但保留分隔符

c# - 是否有与 C# 中的空合并运算符 (??) 等效的 Perl?

arrays - 检查数组中元素的更快方法?

perl - PSGI 响应 : What kinds of filehandles can be expected to work with PSGI, 和 Plack?

perl - 如何在 Perl POD 中仅打印特定格式名称?

mysql - DBIx::新建和创建类错误

arrays - Perl 脚本 - : Useless use of array element in void context at letter_counter. pl lin 38 和 44

linux - git send-email thows Errno architecture (..) does not match executable architecture (...) at/usr/lib64/perl5/Errno.pm line 11