Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^3: File::Glob infinate loop with while loop unlike core glob function

by jpl (Monk)
on Jul 31, 2011 at 17:43 UTC ( [id://917708]=note: print w/replies, xml ) Need Help??


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

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.

Replies are listed 'Best First'.
Re^4: File::Glob infinate loop with while loop unlike core glob function
by Anonymous Monk on Jul 31, 2011 at 21:10 UTC

    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://917708]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-03-19 05:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found