bcrowell2 has asked for the wisdom of the Perl Monks concerning the following question:

O monks,

I observe that the following two examples behave differently:

perl -e 'use File::Glob; while ($x=File::Glob::csh_glob "a.a") {print +"$x\n"; sleep 1} print "$x\n"' perl -e 'use File::Glob; while ($x=File::Glob::bsd_glob "a.a") {print +"$x\n"; sleep 1} print "$x\n"'

The file a.a exists in the current working directory. The bsd_glob version does what I expect, which is to loop forever, but the csh_glob version exits after one iteration. Does anyone understand this? Thanks in advance,

-Ben

Replies are listed 'Best First'.
Re: strange behavior of csh_glob versus bsd_glob
by liverpole (Monsignor) on May 19, 2007 at 19:40 UTC
    Hi bcrowell2,

    If you peek into the source for File::Glob, you'll see that csh_glob is a separate, undocumented method, which is not directly related to the method GLOB_CSH (which is documented, and just sets a bunch of flags).

    Furthermore, there's a comment:

    # NOTE: The glob() export is only here for compatibility with 5.6.0. # csh_glob() should not be used directly, unless you know what you're +doing

    And finally, you'll see that there's a hash %iter (documented as "## borrowed heavily from gsar's File::DosGlob"), which is used the very first time to hold the results from dos_glob, and after that, returns the next value in the already-created list.


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
      Hi all,

      Thanks very much for the replies.

      I originally just used the <> operator, which had the same behavior as csh_glob(). I could just as well have titled this "strange behavior of <> versus bsd_glob." It seems odd to me that <> would have this behavior, but anyway, now I know how to fix the problem: don't use <>, use bsd_glob.

      Thanks! -Ben

Re: strange behavior of csh_glob versus bsd_glob
by Zaxo (Archbishop) on May 19, 2007 at 19:36 UTC

    File::Glob::bsd_glob() just calls the C library function doglob(), which is unlikely to be sensitive to the perl context.

    File::Glob::csh_glob() maintains an iterator over the list of results and does some wantarray checking to deliver either the whole list or one path at a time. That is an occasional perl convention.

    I expect bsd_glob() could be written that way, too. DWIM sometimes means doing what someone else means ;-)

    After Compline,
    Zaxo