具有Glob的Bizarrer Perl行为 [英] Bizarrer perl behavior with glob
本文介绍了具有Glob的Bizarrer Perl行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
use strict;
use warnings;
#only linux
#use diagnostics;
# - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - - -
sub in_function {
my $file = shift;
glob($file) or die ("$file file was not found\n"); #this fails second time called
# this is ok second time called
#my @dummy = glob($file) or die ("$file file was not found\n");
}
# - - - - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - -
my $file = "/tmp/file1*.pdf";
glob($file) or die ("$file file was not found\n");
in_function($file);
$file = "/tmp/file2*.pdf";
glob($file) or die ("$file file was not found\n");
in_function($file);
第一个球很好 第一个in_function glob很好 第二个球很好 第二个in_function glob失败 为什么在函数内调用时需要将glob分配给变量?
First glob is fine First in_function glob is fine Second glob is fine Second in_function glob fails Why do I need to assign glob to a variable when called within a function?
推荐答案
在标量上下文中,glob遍历此类文件名扩展,当列表用完时返回undef.
In scalar context, glob iterates through such filename expansions, returning undef when the list is exhausted.
以下将解决问题:
() = glob($file)
or die ...;
但是您可能想知道匹配项是什么,所以您可以使用
But you problably want to know what the matches are, so you can use
my @matches = glob($file)
or die ...;
这篇关于具有Glob的Bizarrer Perl行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文