gpt4 book ai didi

perl - 如何在 Perl 中使用变量的值作为 glob 模式?

转载 作者:行者123 更新时间:2023-12-02 08:35:01 27 4
gpt4 key购买 nike

在 Perl 中,您可以获得与模式匹配的文件列表:

my @list = <*.txt>;
print "@list";

现在,我想将模式作为变量传递(因为它被传递到函数中)。但这不起作用:

sub ProcessFiles {
my ($pattern) = @_;
my @list = <$pattern>;
print "@list";
}

readline() on unopened filehandle at ...

有什么建议吗?

最佳答案

使用glob :

use strict;
use warnings;

ProcessFiles('*.txt');

sub ProcessFiles {
my ($pattern) = @_;
my @list = glob $pattern;
print "@list";
}

以下是您收到警告的原因的解释,来自 I/O Operators :

If what the angle brackets contain is a simple scalar variable (e.g., $foo), then that variable contains the name of the filehandle to input from... it's considered cleaner to call the internal function directly as glob($foo), which is probably the right way to have done it in the first place.)

关于perl - 如何在 Perl 中使用变量的值作为 glob 模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2263933/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com