perl glob 检查带前缀的文件是否存在
perl glob check if file with prefix exists
我在使用 glob
搜索文件时遇到一些奇怪的行为。
(我认为它是有意的,但我不想要它)
我有 2 个文件,分别名为 aaa:bbb
和 aaa:ccc
。
我有代码可以查看前缀为 aaa:*
的文件是否存在。我使用以下代码执行此操作:
my $filelocation_special = "aaa";
if (glob($filelocation_special . ":*")) {
# file(s) exists!
}
这行得通。但是,如果我 运行 同样的代码再重复两次 glob returns undefined.
这些是我的 returns:(大部分时间都是)
print STDERR "glob: " . glob($filelocation_special . ":*") . "\n";
# 1 run: aaa:bbb
# 2 run: aaa:ccc
# 3 run:
如何重置 glob
始终只检查是否存在具有此前缀的文件?
也许我应该完全使用一些不同的检查,但我似乎找不到任何快速的方法,只是检查文件是否存在。
if (glob(...))
是 call to glob
in scalar context.
In scalar context, glob iterates through
such filename expansions, returning undef when the list is
exhausted.
所以每次你在标量上下文中调用 glob
时,使用相同的参数,并且 来自同一行代码 ,你会得到不同的结果,最终当与您的参数匹配的文件列表用完时,您将得到 undef
。
要让您的表达式表达 "if there are files that match this pattern ...",您需要以某种方式使用列表上下文。
@tmp = glob(pattern)
if (@tmp)
if (()=glob(pattern))
if (@{[glob(pattern)]})
转移:这些中的任何一个也有效(因为@*
是许多标点符号值的有效全局数组标识符*
)
if (@@=glob(pattern))
if (@<=glob(pattern))
if (@:=glob(pattern))
它们甚至可以写成
if (@ <= glob(pattern))
if (@ := glob(pattern))
因此建议了 secret operators 的更多候选人。
@@ = EXPR like the goatse operator, returns a count of elements
@ <= EXPR in EXPR when EXPR is evaluated in list context; useful
@ := EXPR when EXPR in scalar context does not do this
我在使用 glob
搜索文件时遇到一些奇怪的行为。
(我认为它是有意的,但我不想要它)
我有 2 个文件,分别名为 aaa:bbb
和 aaa:ccc
。
我有代码可以查看前缀为 aaa:*
的文件是否存在。我使用以下代码执行此操作:
my $filelocation_special = "aaa";
if (glob($filelocation_special . ":*")) {
# file(s) exists!
}
这行得通。但是,如果我 运行 同样的代码再重复两次 glob returns undefined.
这些是我的 returns:(大部分时间都是)
print STDERR "glob: " . glob($filelocation_special . ":*") . "\n";
# 1 run: aaa:bbb
# 2 run: aaa:ccc
# 3 run:
如何重置 glob
始终只检查是否存在具有此前缀的文件?
也许我应该完全使用一些不同的检查,但我似乎找不到任何快速的方法,只是检查文件是否存在。
if (glob(...))
是 call to glob
in scalar context.
In scalar context, glob iterates through such filename expansions, returning undef when the list is exhausted.
所以每次你在标量上下文中调用 glob
时,使用相同的参数,并且 来自同一行代码 ,你会得到不同的结果,最终当与您的参数匹配的文件列表用完时,您将得到 undef
。
要让您的表达式表达 "if there are files that match this pattern ...",您需要以某种方式使用列表上下文。
@tmp = glob(pattern)
if (@tmp)
if (()=glob(pattern))
if (@{[glob(pattern)]})
转移:这些中的任何一个也有效(因为
@*
是许多标点符号值的有效全局数组标识符*
)
if (@@=glob(pattern))
if (@<=glob(pattern))
if (@:=glob(pattern))
它们甚至可以写成
if (@ <= glob(pattern))
if (@ := glob(pattern))
因此建议了 secret operators 的更多候选人。
@@ = EXPR like the goatse operator, returns a count of elements
@ <= EXPR in EXPR when EXPR is evaluated in list context; useful
@ := EXPR when EXPR in scalar context does not do this