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


in reply to Re: File::Glob infinate loop with while loop unlike core glob function
in thread File::Glob infinite loop with while loop unlike core glob function

No, I don't think it is a bug. glob returns a list

This is the bug, that it returns a list in scalar context, and the docs don't warn about it.

perldoc -f glob says

In list context, returns a (possibly empty) list of filename expansions on the value of EXPR such as the standard Unix shell /bin/csh would do. In scalar context, glob iterates through such filename expansions, returning undef when the list is exhausted.

...

Beginning with v5.6.0, this operator is implemented using the standard "File::Glob" extension. See File::Glob for details, including "bsd_glob" which does not treat whitespace as a pattern separator.

use File::Glob ':glob'; overrides glob (csh_glob) with a function that behave differently in scalar context (bsd_glob), and the documentation dosn't warn you explicitly.

making bsd_glob DWIM in scalar context is trivial (just copy/paste from csh_glob)

fixing the pod is equally trivial.

No, I'm not contemplating filing a bug report, Gulliver should :)

  • Comment on Re^2: File::Glob infinate loop with while loop unlike core glob function
  • Download Code

Replies are listed 'Best First'.
Re^3: File::Glob infinate loop with while loop unlike core glob function
by jpl (Monk) on Jul 31, 2011 at 17:43 UTC
    Beginning with v5.6.0, this operator is implemented using the standard "File::Glob" extension. See File::Glob for details, including "bsd_glob" which does not treat whitespace as a pattern separator.
    When I look at File::Glob, I don't see it promising to do "while magic". I see it "returning a list". If the core glob took advantage of this functionality to determine the list it is ready to do "while magic" over, that is a decision made by the core, not (necessarily) a requirement on File::Glob. Regardless of how File::Glob might have been "improved" in the v5.6.0 era, it has been behaving in the stated way since then, and it is not "trivial" to modify that behavior now. That could break existing code, and the porters are notoriously (and justifiably) reluctant to do that.

    I agree that emphasizing the absence of "while magic" in the File::Glob documentation could be helpful. I confess to having wondered what was causing the problem with gulliver's code, until I recalled that core glob can do what it does only by "magic". I'm not a big fan of "magic", in part because of problems like this.

      When I look at File::Glob, I don't see it promising to do "while magic".

      I never claimed it promised to do while magic, surely you can see that.

      You can also see that there no magic involved (just wantarray) if you UTSL, GLOB_NOMAGIC option notwithstanding.

       

      and it is not "trivial" to modify that behavior now.

      When I used "trivial" , it was obvious I was talking about the effort (copy/paste) and not the politics;

      But yes, even the politics are "trivial" because File::Glob is a module, it already changes behaviour , it can do so again

      Cwd/File::Spec have changed behavior since 5.6.0, so can File::Glob; it doesn't have to live in CORE, it can become dual-lived like Cwd

       

      That could break existing code, and the porters are notoriously (and justifiably) reluctant to do that.

      If fixing an oversight breaks existing code, its worth any potential fallout

      It doesn't even have to break existing code, just make it do the right thing via ':globDWIM' or ':globwantarray' or ':globscalar' .. whatever tag makes most sense

      And AFAIC :) code that relies on the existing behaviour of a-list-in-scalar-context is unimportant and/or already broken ;) so it won't be affected much by a change :D

        The more I look at this, the more I'm coming around to your point of view. When I look at

        ext/File-Glob/t/global.t

        they appear to be doing extensive testing to make sure a glob overload behaves in the same way as the core glob, including loops using <> and explicit invocations of glob. If I modify global.t as follows

        4,5c4 < chdir 't' if -d 't'; < @INC = '../lib'; --- > chdir '/home/jpl/src/perl-5.14.1/t'; 94a94,111 > > @s = (); > while (glob("/tmp/dir with spaces/*")) { > push @s, $_; > last if (@s > 1000); > } > print(scalar(@s), "\n"); > print(join(" ", @s), "\n"); > > package Bar; > use File::Glob ':glob'; > @s = (); > while (glob("/tmp/dir with spaces/*")) { > push @s, $_; > last if (@s > 1000); > } > print(scalar(@s), "\n"); > # print(join(" ", @s), "\n");
        I get
        2 /tmp/dir with 1001
        at the end of the output (the directory has 3 entries). It's above my pay grade to figure out why this happens, or how to make the replacement using :glob behave as core glob does, but I'm running out of arguments why the current behavior is correct. Your suggestion of a new tag to get this behavior seems reasonable.
        You can also see that there no magic involved (just wantarray) if you UTSL, GLOB_NOMAGIC option notwithstanding.
        The "magic" is not in detecting the context in which you are invoked, it is in returning different scalar values when invoked with the same argument. This is not your math teacher's concept of a function. If you run this
        #!/usr/bin/perl -w use strict; use feature 'say'; say scalar(glob('*')); say scalar(glob('*')); say scalar(glob('*')) for (1..2);
        in a directory with more than one file, you'll get the same file, twice, then the same file and a different one. There's obviously some "hidden state" beyond scalar context. I choose to call this "magic", you can choose otherwise.