perl - Perl readdir 警告 - 名称“main::DIR”仅使用一次:可能的错字
问题描述
我有一个脚本,它列出了特定目录中的可能文件。该代码工作正常,但如何避免此警告?
#!/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);
推荐阅读
- django - How to fetch data from one model and display it in the form as drop down for storing it in another model in django
- android - How do I test this Custom UI Class to have code coverage
- sql - Using MAX() without grouping by
- angular - How to store ion-range slider value in local storage on change using ionic-v4?
- laravel - "Missing required parameters for [Route: property.edit]" error when editing product
- dart - How to Create a Customised BottomTabBar
- python - How to control WINDOW download dialga-box open/save/cancel with Selenium in Python
- javascript - 带有过滤管的反应形式随机缺少表单控件
- dart - 如何创建基础工厂并在 Flutter 中的子类上覆盖它
- python - 无法将类型字符变化 [] 转换为 jsonb django 迁移