为什么glob lstat匹配条目? [英] Why does glob lstat matching entries?
问题描述
在此问题中研究行为时,我很惊讶地看到perl
Looking into behavior in this question, I was surprised to see that perl lstat()
s every path matching a glob pattern:
$ mkdir dir
$ touch dir/{foo,bar,baz}.txt
$ strace -e trace=lstat perl -E 'say $^V; <dir/b*>'
v5.10.1
lstat("dir/baz.txt", {st_mode=S_IFREG|0664, st_size=0, ...}) = 0
lstat("dir/bar.txt", {st_mode=S_IFREG|0664, st_size=0, ...}) = 0
我在使用glob(pattern)
和<pattern>
以及更高版本的perl的Linux系统上看到相同的行为.
I see the same behavior on my Linux system with glob(pattern)
and <pattern>
, and with later versions of perl.
我的期望是,遍历只会在幕后的opendir/readdir中进行,而无需检查正在搜索的实际路径名.
My expectation was that the globbing would simply opendir/readdir under the hood, and that it would not need to inspect the actual pathnames it was searching.
此lstat
的目的是什么?它会影响glob()的返回吗?
What is the purpose of this lstat
? Does it affect the glob()s return?
推荐答案
此奇怪的行为已经之前已注意到在PerlMonks上.事实证明,glob
调用lstat
以支持其 GLOB_MARK
标志,其效果是:
This strange behavior has been noticed before on PerlMonks. It turns out that glob
calls lstat
to support its GLOB_MARK
flag, which has the effect that:
作为与模式匹配的目录的每个路径名都附加了斜杠.
Each pathname that is a directory that matches the pattern has a slash appended.
要查明目录条目是否引用子目录,您需要stat
它.即使没有给出标志,显然也可以做到这一点.
To find out whether a directory entry refers to a subdir, you need to stat
it. This is apparently done even when the flag is not given.
这篇关于为什么glob lstat匹配条目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!