Fra*_*ank 5 perl design-patterns glob file
在Perl中,您可以获得与模式匹配的文件列表:
my @list = <*.txt>;
print "@list";
Run Code Online (Sandbox Code Playgroud)
现在,我想将模式作为变量传递(因为它传递给函数).但这不起作用:
sub ProcessFiles {
my ($pattern) = @_;
my @list = <$pattern>;
print "@list";
}
readline() on unopened filehandle at ...
Run Code Online (Sandbox Code Playgroud)
有什么建议?
too*_*lic 12
使用glob:
use strict;
use warnings;
ProcessFiles('*.txt');
sub ProcessFiles {
my ($pattern) = @_;
my @list = glob $pattern;
print "@list";
}
Run Code Online (Sandbox Code Playgroud)
以下是对I/O操作员发出警告的原因的解释:
如果尖括号包含的是一个简单的标量变量(例如,$ foo),那么该变量包含要从中输入的文件句柄的名称...它被认为是更清晰的直接调用内部函数为glob($ foo),可能是首先完成它的正确方法.)