|XP is just a number|
Re: Using grep and glob to find directories containing fileby 7stud (Deacon)
|on Feb 03, 2013 at 17:28 UTC||Need Help??|
This behavior is certainly not obvious and it is not (clearly) documented either under 'perldoc -f glob' or at perldoc.perl.org. The line saying "In scalar context, glob iterates through such filename expansions, returning undef when the list is exhausted" does not make it clear (at least to me) that such a state persists across calls to glob with a new argument!
I agree that the docs for glob (perldoc -f glob) are not clear. When I read the docs for glob, and I came upon the sentence:
In scalar context, glob iterates through such filename expansions, returning undef when the list is exhausted.
I had no idea what that meant. However, I immediately recognized that that sentence did NOT mean what you claimed it meant, namely that in scalar context glob returns true if there were any matching files. I have no idea how you arrived at that interpretation. In my opinion, the literal interpretation would be that in scalar context, glob() sits for a few seconds as it spins through the list of matches, and then glob returns undef, i.e. glob always returns undef in scalar context.
In any case, after reading the docs I was prompted to try an experiment to see how glob() works in scalar context. So I setup this directory structure:
/some_dir my_prog.pl dir1/ a.txt b.txt f1.txt f2.txt dir2/ dir3/ x.txt y.txtAnd then I ran this code:
After examining the output, I quickly understood how glob() works in scalar context. To be clearer, the docs should say something like this:
In scalar context, glob returns the next filename from the list of matching filenames or undef if the list has been exhausted.
Edit-- so that statement should have some qualifications:
In scalar context: