开发者

problem with `glob` function in perl

开发者 https://www.devze.com 2023-03-30 02:56 出处:网络
I want to get a list of all the files whose name looks like : $res_dir/$line/$fub_name*.istf meaning: in directory $dir in sub directory $line a file that starts with $name and ends with .istf.

I want to get a list of all the files whose name looks like : $res_dir/$line/$fub_name*.istf

meaning: in directory $dir in sub directory $line a file that starts with $name and ends with .istf.

eac开发者_JAVA技巧h sub directory contains only 1 such file, but there are many sub directories. I've written this:

foreach $line (@arr){
    chomp ($line);
    $new_istf = glob ("$res_dir/$line/$fub_name*.istf");
    do something...

but it doesn't give me all the files, but only half of them. when I added the fallowing debug code:

if ($new_istf){
    print "\tdbug:: $res_dir/$line/$fub_name*.istf:\t'$new_istf'\n";
} else {
    print "\tdbug:: $res_dir/$line/$fub_name*.istf:\t''\n";
}

and the resulting output was:

    dbug:: RESULTS//791202_0_SA0/ieuni5cts*.istf:   'RESULTS//791202_0_SA0/ieuni5cts.mlc.xxx.x.istf'
    dbug:: RESULTS//791212_0_SA0/ieuni5cts*.istf:   ''
    dbug:: RESULTS//791241_0_SA0/ieuni5cts*.istf:   'RESULTS//791241_0_SA0/ieuni5cts.mlc.xxx.x.istf'
    dbug:: RESULTS//791248_0_SA0/ieuni5cts*.istf:   ''
    dbug:: RESULTS//791251_0_SA0/ieuni5cts*.istf:   'RESULTS//791251_0_SA0/ieuni5cts.mlc.xxx.x.istf'
    dbug:: RESULTS//791258_0_SA0/ieuni5cts*.istf:   ''
    dbug:: RESULTS//791272_0_SA0/ieuni5cts*.istf:   'RESULTS//791272_0_SA0/ieuni5cts.mlc.xxx.x.istf'
    dbug:: RESULTS//791273_0_SA1/ieuni5cts*.istf:   ''
    dbug:: RESULTS//791301_0_SA0/ieuni5cts*.istf:   'RESULTS//791301_0_SA0/ieuni5cts.mlc.xxx.x.istf'
    dbug:: RESULTS//791333_0_SA0/ieuni5cts*.istf:   ''
    dbug:: RESULTS//791349_0_SA0/ieuni5cts*.istf:   'RESULTS//791349_0_SA0/ieuni5cts.mlc.xxx.x.istf'
    dbug:: RESULTS//791357_0_SA0/ieuni5cts*.istf:   ''
    dbug:: RESULTS//791405_0_SA1/ieuni5cts*.istf:   'RESULTS//791405_0_SA1/ieuni5cts.mlc.xxx.x.istf'
    dbug:: RESULTS//791432_0_SA0/ieuni5cts*.istf:   ''
    dbug:: RESULTS//791434_0_SA0/ieuni5cts*.istf:   'RESULTS//791434_0_SA0/ieuni5cts.mlc.xxx.x.istf'
    dbug:: RESULTS//791459_0_SA0/ieuni5cts*.istf:   ''
    dbug:: RESULTS//791462_0_SA0/ieuni5cts*.istf:   'RESULTS//791462_0_SA0/ieuni5cts.mlc.xxx.x.istf'
    dbug:: RESULTS//791463_0_SA0/ieuni5cts*.istf:   ''
    dbug:: RESULTS//791467_0_SA0/ieuni5cts*.istf:   'RESULTS//791467_0_SA0/ieuni5cts.mlc.xxx.x.istf'
    dbug:: RESULTS//791471_0_SA1/ieuni5cts*.istf:   ''

but all those files exist. since glob suppose to work like the sell interpreter, i've checked it and when I do echo RESULTS//791463_0_SA0/ieuni5cts*.istf (and every other file that was not detected by glob, it gives the right file name.

I've also tried to run glob as a one liner with the paths that didn't give result, and it worked:

perl -e 'print glob("RESULTS//791467_0_SA0/ieuni5cts*.istf"),"\n"'

and it gave the right result as well

Where is the problem in my code?


From perldoc -f glob:


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. In scalar context, glob iterates through such filename expansions, returning undef when the list is exhausted.


The code posted is in scalar context.

See if calling it in list context solves the issue:

my ( $new_istf ) = glob ( "$res_dir/$line/$fub_name*.istf" );
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号