http://www.perlmonks.org?node_id=1016841


in reply to Using grep and glob to find directories containing file

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.txt
And then I ran this code:
use strict; use warnings; use 5.012; while (my $x = glob "dir1/f*") { say $x; } --output:-- dir1/f1.txt dir1/f2.txt

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:

  1. Inside a loop: glob returns the next filename from the list of matching filenames, or undef if the list has been exhausted--with the next call to glob returning the first matching filename again.
  2. Outside a loop: glob() always returns the first matching filename.

Replies are listed 'Best First'.
Re^2: Using grep and glob to find directories containing file
by Anonymous Monk on Feb 03, 2013 at 19:17 UTC
    I agree with your final revision to the documentation string. The doc text truly doesn't make much literal sense. And unless one has perfect Perl Monk karma, I don't see how one can easily intuit the difference between scalar context in a looped vs. non-looped context. The purpose of documentation is (presumably) to help those who are not yet experts. In this case, I humbly propose that the documentation fails to adequately and properly document the behavior in scalar context.

    Also what happens when glob is called in a function that is embedded in a loop? Either way I can imagine challenges. If it is still considered in a loop then the behavior for example of glob used somewhere deep in a module funciton would vary depending on whether it was at some level called from something in a loop. On the other hand if calling it from a function that is embedded in a loop behaves differently from calling directly, then again you have an odd behavior where simply wrapping 'glob' in a function call would change its behavior.

    To me, this still seems quite flaky and upnredictable. At a minimum, it deserves copious documentation to explain the behavior and potential issues.