Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

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

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


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

No, I don't think it is a bug. glob returns a list. In scalar content, as while establishes, you just get the last element of the list. In this case, it is a non-empty string, so it acts as true, and it never changes, so there is an infinite loop. If you do
@files = glob "$folder/*.txt";
the list, in array context, turns into a real array, over which you can iterate with your favorite iterator.

Replies are listed 'Best First'.
Re^2: File::Glob infinite loop with while loop unlike core glob function
by Gulliver (Monk) on Jul 29, 2011 at 18:11 UTC

    I didn't say it was a bug. Perhaps you intended to reply to the Anon Poster.

    From Programming Perl, 3rd edition:

    Whether you use the glob function or the old angle-bracket form, the fileglob operator also does while magic like the line input operator, assigning the result to $_. (That was the rationale for overloading the angle operator in the first place.)

    I expected to be able to just put the use File::Glob ':glob' at the top of my existing code to fix the whitespace issue.

      The comment, was, indeed, targeted at the anonymous poster (or anyone else contemplating filing a bug report). I can see where the snippet you quote could be misleading. The glob core function does do "while magic". I suspect that is a decision they wish they could undo.
Re^2: File::Glob infinate loop with while loop unlike core glob function
by Anonymous Monk on Jul 29, 2011 at 23:41 UTC

    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 :)

      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

Log In?
Username:
Password:

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

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

    No recent polls found