Using if glob
is wrong.
$ ls
a.log
b.log
$ perl -Mv5.14 -e'
sub test { say "$_[0]: ", glob( $_[0] ) ? "at least one match" : "no matches" }
test( "*.log" );
test( "*.txt" );
test( "*.log" );
'
*.log: at least one match # Correct
*.txt: at least one match # Incorrect
*.log: no matches # Incorrect and a contradiction
glob
in scalar context acts as an iterator.
- The first time it's called, it returns the first match.
- The second time it's called, it returns the second match (regardless of the argument).
- When it's called after all results have been returned, it returns
undef
.
If you're going to use glob
, you're going to have to get all the results, not just the first.
For example, the following uses the scalar( () = ... )
trick to call glob
in list context (getting all the results) and count the number of items it returns.
$ perl -Mv5.14 -e'
sub test { say "$_[0]: ", ( () = glob( $_[0] ) ) ? "at least one match" : "no matches" }
test( "*.log" );
test( "*.txt" );
test( "*.log" );
'
*.log: at least one match
*.txt: no matches
*.log: at least one match