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.


In reply to Re: Using grep and glob to find directories containing file by 7stud
in thread Using grep and glob to find directories containing file by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.