如何在 Perl 中使用变量的值作为 glob 模式?
在 Perl 中,您可以获得与模式匹配的文件列表:
my @list = <*.txt>;
print "@list";
现在,我想将模式作为变量传递(因为它被传递到函数中)。但这不起作用:
sub ProcessFiles {
my ($pattern) = @_;
my @list = <$pattern>;
print "@list";
}
readline() on unopened filehandle at ...
有什么建议吗?
In Perl, you can get a list of files that match a pattern:
my @list = <*.txt>;
print "@list";
Now, I'd like to pass the pattern as a variable (because it's passed into a function). But that doesn't work:
sub ProcessFiles {
my ($pattern) = @_;
my @list = <$pattern>;
print "@list";
}
readline() on unopened filehandle at ...
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 glob:
以下是为什么收到警告的解释,来自 I/O 运算符:
Use glob:
Here is an explanation for why you get the warning, from I/O Operators:
为什么不将文件列表的数组引用传递给函数?
Why not pass the array reference of the list of files to the function?
用“eval”命令包装它怎么样?像这样...
What about wrapping it with an "eval" command? Like this...