当给定一个不带glob字符的字符串时,为什么Perl的glob()函数总是返回文件名? [英] Why does Perl's glob() function always return a file name when given a string with no globbing characters?

查看:98
本文介绍了当给定一个不带glob字符的字符串时,为什么Perl的glob()函数总是返回文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我给Perl的glob函数提供了一个清单列表和一个字符串.全局问题已按预期方式处理,但始终可以找到字符串.例如:

I gave a list of globs and one string to Perl's glob function. The globs were treated as expected but the string is always found. For example:

$ ls
foo
$ perl -le '@files=glob("*bar"); print @files' ## prints nothing, as expected
$ perl -le '@files=glob("bar"); print @files'
bar

如上所示,即使没有这样的文件,第二个示例也会打印bar.

As you can see above, the second example prints bar even though no such file exists.

我的第一个想法是,它的行为类似于shell,因为当没有扩展可用时,一个glob(或被视为glob的东西)会自行扩展.例如,在csh中(很糟糕,这就是Perl的glob()函数所遵循的,请参见下面的引号):

My first thought is that it behaves like the shell in that when no expansion is available, a glob (or something being treated as a glob) expands to itself. For example, in csh (awful as it is, this is what Perl's glob() function seems to be following, see the quote below):

% foreach n (*bar*)
foreach: No match.

% foreach n (bar)
foreach? echo $n
foreach? end
bar                     ## prints the string

但是,根据文档glob应该返回文件名扩展名(强调我的意思):

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 foo
execve("/home/mcarey/perl5/perlbrew/perls/5.24.0-debug/bin/perl", ["perl", "-eglob \"foo\""], [/* 39 vars */]) = 0
lstat("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 returned
  • if 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()函数总是返回文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆