问题
my test script:
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";
I want to save the file names in $loc into array for further processing, but its not doing so, what am I missing? output is:
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:\>
回答1:
You want to list the .log
files in the following share:
\\ant\d1_sp\test__18716093
To do so, you need to use the following glob pattern:
\\\\ant\\d1_sp\\test__18716093\\*.log
The following is a string literal that produces that string:
"\\\\\\\\ant\\\\d1_sp\\\\test__18716093\\\\*.log"
So the solution is
glob("\\\\\\\\ant\\\\d1_sp\\\\test__18716093\\\\*.log")
Best to just use /
instead of \
since you don't need to escape it in either glob patterns or string literals.
glob("//ant/d1_sp/test__18716093/*.log")
回答2:
I'm pretty sure you can't use glob
on a network drive, however opendir
and readdir
will work
Like this
my $loc = '\\\\ant\d1_sp\test__18716093';
my @test = do {
opendir my ($dh), $loc or die $!;
map "$loc\\$_", grep /\.log$/i, readdir $dh;
};
回答3:
For one thing, your qq
supplies extra quoting that glob
may or may not handle as intended.
This correctly lists all .pl
files in $loc
, being single directory or '{dir1,dir2,...}'
my @files = glob "$loc/*.pl";
With an extra qq
I get the empty list, on v5.10.
Here is an example with extra qq
brought up in a comment by ThisSuitIsBlackNot, copied
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`.
This works as intended on v5.16 but on v5.10 it does not (empty list), while without qq
it does. This inconsistent behavior of glob
with respect to quoting and space was fixed in v5.16.
With spaces in the path an extra level of quoting is necessary, as noted by Borodin and explained by ThisSuitIsNotBlack in comments, summarized here.
For $path
with spaces one needs either of
$loc = q("$path"); glob("$loc/*");
$loc = "$path"; glob(qq{"$loc/*"});
Another option, which I would prefer, is to use Path::Class.
From 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.
From perldoc perlop
, while talking about <..>
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...
[No more interpolation is mentioned.]
来源:https://stackoverflow.com/questions/35999388/how-to-use-perl-glob-to-read-remote-location