如何使用 Perl glob 读取远程位置?
How to use Perl glob to read remote location?
我的测试脚本:
my $loc = "\\ant\d1_sp\test__18716093";
####$loc = "c:\temp"; #this works good if I un-comment.
system("dir $loc\*.log"); #added system command just for debugging.
my @test = glob qq("$loc\*.log");
print "\narray=@test\n";
我想将 $loc 中的文件名保存到数组中以供进一步处理,但它没有这样做,我错过了什么?输出是:
C:\>perl c:\temp\foo.pl
Directory of \ant\d1_sp\test__18716093
03/14/2016 01:09 PM 959 build_1.8980.log
03/14/2016 01:20 PM 102,402 build_2.98981.log
2 File(s) 103,361 bytes
0 Dir(s) 1,589,522,239,488 bytes free
array=
C:\>
我很确定你不能在网络驱动器上使用 glob
,但是 opendir
和 readdir
可以工作
像这样
my $loc = '\\ant\d1_sp\test__18716093';
my @test = do {
opendir my ($dh), $loc or die $!;
map "$loc\$_", grep /\.log$/i, readdir $dh;
};
您想在以下共享中列出 .log
个文件:
\ant\d1_sp\test__18716093
为此,您需要使用以下 glob 模式:
\\ant\d1_sp\test__18716093\*.log
以下是生成该字符串的字符串文字:
"\\\\ant\\d1_sp\\test__18716093\\*.log"
所以解决方案是
glob("\\\\ant\\d1_sp\\test__18716093\\*.log")
最好只使用 /
而不是 \
,因为您不需要在 glob 模式或字符串文字中转义它。
glob("//ant/d1_sp/test__18716093/*.log")
一方面,您的 qq
提供了额外的引用,glob
可能会或可能不会按预期处理。
这正确列出了 $loc
中的所有 .pl
个文件,是单个目录或 '{dir1,dir2,...}'
my @files = glob "$loc/*.pl";
有了额外的 qq
我在 v5.10 上得到了空列表。
这里有一个例子,在 ThisSuitIsBlackNot 的评论中提出了额外的 qq
,已复制
mkdir foo && touch foo/{bar,baz,qux}.pl;
perl -E '$loc = "foo"; say for glob qq{"$loc/*.pl"}'
# prints `foo/bar.pl, foo/baz.pl, and foo/qux.pl`.
这在 v5.16 上按预期工作,但在 v5.10 上不起作用(空列表),而在没有 qq
的情况下它起作用。 glob
在引用和 space 方面的这种不一致行为是 fixed in v5.16。
在路径中使用 spaces 时,需要额外的引用级别,正如 Borodin and explained by ThisSuitIsNotBlack 在评论中指出的那样,总结于此。
对于 $path
和 space,需要
之一
$loc = q("$path"); glob("$loc/*");
$loc = "$path"; glob(qq{"$loc/*"});
另一个我更喜欢的选项是使用 Path::Class.
来自 perldoc -f glob
:
This is the internal function implementing the <*.c>
operator, but you can use it directly. If EXPR is omitted, $_
is used. The <*.c>
operator is discussed in more detail in "I/O Operators" in perlop.
来自perldoc perlop
,同时谈论<..>
If what's within the angle brackets is neither a filehandle nor a simple
scalar variable containing a filehandle name, typeglob, or typeglob
reference, it is interpreted as a filename pattern to be globbed, and
either a list of filenames or the next filename in the list is returned,
depending on context.
...
One level of double-quote interpretation is done first...
[插值不再赘述]
我的测试脚本:
my $loc = "\\ant\d1_sp\test__18716093";
####$loc = "c:\temp"; #this works good if I un-comment.
system("dir $loc\*.log"); #added system command just for debugging.
my @test = glob qq("$loc\*.log");
print "\narray=@test\n";
我想将 $loc 中的文件名保存到数组中以供进一步处理,但它没有这样做,我错过了什么?输出是:
C:\>perl c:\temp\foo.pl
Directory of \ant\d1_sp\test__18716093
03/14/2016 01:09 PM 959 build_1.8980.log
03/14/2016 01:20 PM 102,402 build_2.98981.log
2 File(s) 103,361 bytes
0 Dir(s) 1,589,522,239,488 bytes free
array=
C:\>
我很确定你不能在网络驱动器上使用 glob
,但是 opendir
和 readdir
可以工作
像这样
my $loc = '\\ant\d1_sp\test__18716093';
my @test = do {
opendir my ($dh), $loc or die $!;
map "$loc\$_", grep /\.log$/i, readdir $dh;
};
您想在以下共享中列出 .log
个文件:
\ant\d1_sp\test__18716093
为此,您需要使用以下 glob 模式:
\\ant\d1_sp\test__18716093\*.log
以下是生成该字符串的字符串文字:
"\\\\ant\\d1_sp\\test__18716093\\*.log"
所以解决方案是
glob("\\\\ant\\d1_sp\\test__18716093\\*.log")
最好只使用 /
而不是 \
,因为您不需要在 glob 模式或字符串文字中转义它。
glob("//ant/d1_sp/test__18716093/*.log")
一方面,您的 qq
提供了额外的引用,glob
可能会或可能不会按预期处理。
这正确列出了 $loc
中的所有 .pl
个文件,是单个目录或 '{dir1,dir2,...}'
my @files = glob "$loc/*.pl";
有了额外的 qq
我在 v5.10 上得到了空列表。
这里有一个例子,在 ThisSuitIsBlackNot 的评论中提出了额外的 qq
,已复制
mkdir foo && touch foo/{bar,baz,qux}.pl;
perl -E '$loc = "foo"; say for glob qq{"$loc/*.pl"}'
# prints `foo/bar.pl, foo/baz.pl, and foo/qux.pl`.
这在 v5.16 上按预期工作,但在 v5.10 上不起作用(空列表),而在没有 qq
的情况下它起作用。 glob
在引用和 space 方面的这种不一致行为是 fixed in v5.16。
在路径中使用 spaces 时,需要额外的引用级别,正如 Borodin and explained by ThisSuitIsNotBlack 在评论中指出的那样,总结于此。
对于 $path
和 space,需要
$loc = q("$path"); glob("$loc/*");
$loc = "$path"; glob(qq{"$loc/*"});
另一个我更喜欢的选项是使用 Path::Class.
来自 perldoc -f glob
:
This is the internal function implementing the
<*.c>
operator, but you can use it directly. If EXPR is omitted,$_
is used. The<*.c>
operator is discussed in more detail in "I/O Operators" in perlop.
来自perldoc perlop
,同时谈论<..>
If what's within the angle brackets is neither a filehandle nor a simple scalar variable containing a filehandle name, typeglob, or typeglob reference, it is interpreted as a filename pattern to be globbed, and either a list of filenames or the next filename in the list is returned, depending on context.
...
One level of double-quote interpretation is done first...
[插值不再赘述]