However, according to the docs, glob should return filename expansions (emphasis mine): 在列表上下文中,返回EXPR值的文件名扩展列表(可能为空),例如标准Unix shell/bin/csh会执行的操作. In list context, returns a (possibly empty) list of filename expansions on the value of EXPR such as the standard Unix shell /bin/csh would do.那么当传递给glob的参数中没有浮点字符时,为什么会返回自身?这是错误还是我做错了什么?So why is it returning itself when there are no globbing characters in the argument passed to glob? Is this a bug or am I doing something wrong?推荐答案我想我希望Perl在后台检查文件是否存在.I guess I expected Perl to be checking for file existence in the background. Perl 正在检查文件是否存在:Perl is checking for file existence:$ strace perl -e'glob "foo"' 2>&1 | grep fooexecve("/home/mcarey/perl5/perlbrew/perls/5.24.0-debug/bin/perl", ["perl", "-eglob \"foo\""], [/* 39 vars */]) = 0lstat("foo", {st_mode=S_IFREG|0664, st_size=0, ...}) = 0那么当传递给glob的参数中没有glob字符时为什么会返回自身?So why is it returning itself when there are no globbing characters in the argument passed to glob?因为那是csh所做的. Perl的glob实现基于 glob(3)启用了GLOB_NOMAGIC标志:Because that's what csh does. Perl's implementation of glob is based on glob(3) with the GLOB_NOMAGIC flag enabled: GLOB_NOMAGIC与GLOB_NOCHECK相同,但仅在不包含任何特殊字符*,?或[的情况下才追加模式.提供GLOB_NOMAGIC是为了简化实现历史性的csh(1)全局行为,并且可能不应该在其他任何地方使用.Is the same as GLOB_NOCHECK but it only appends the pattern if it does not contain any of the special characters *, ? or [. GLOB_NOMAGIC is provided to simplify implementing the historic csh(1) globbing behavior and should probably not be used anywhere else. GLOB_NOCHECK如果pattern与任何路径名都不匹配,则glob()返回仅包含pattern的列表...If pattern does not match any pathname, then glob() returns a list consisting of only pattern...因此,对于像foo这样的没有通配符的模式:So, for a pattern like foo with no wildcards:如果存在匹配的文件,则返回文件名扩展名(foo)如果不存在匹配文件,则返回模式(foo)if a matching file exists, the filename expansion (foo) is returnedif no matching file exists, the pattern (foo) is returned由于文件名扩展名与模式相同,Since the filename expansion is the same as the pattern,glob 'foo'不论文件foo是否存在,列表上下文中的总是返回带有单个元素foo的列表.in list context will always return a list with the single element foo, whether the file foo exists or not. 这篇关于当给定一个不带glob字符的字符串时,为什么Perl的glob()函数总是返回文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-15 04:34