develooper Front page | perl.perl5.porters | Postings from September 2013

Re: [perl #119785] bsd_glob does not sort case-sensitively

Thread Previous | Thread Next
From:
John Wiersba
Date:
September 16, 2013 22:43
Subject:
Re: [perl #119785] bsd_glob does not sort case-sensitively
Message ID:
1379371373.83669.YahooMailNeo@web162801.mail.bf1.yahoo.com
> Jim Keenan wrote:
> I think we should ask:
>
> 1. Is this behavior expected and correct?
>
> 2. If it is, what changes do we have to make in 'ext/File-Glob/Glob.pm'
> so that a user of File::Glob will have the correct expectations?

Now that I finally understand the File::Glob doc, I think the results are correct (i.e. they match the documentation).

1) bsd_glob(PATTERN) === bsd_glob(PATTERN, GLOB_CSH)


Relative to sorting, 
2) GLOB_CSH === GLOB_ALPHASORT

GLOB_NOCASE affects the interpretation of PATTERN, so is not applicable in this case, but when it is specified, it turns off the default use of GLOB_CSH, thereby reverting to the default mentioned in the GLOB_NOSORT paragraph, namely ASCII sort rather than ALPHASORT.

GLOB_NOSORT returns a more or less randomly ordered list (depending on the physical order of the entries in the directory.  You're getting the result you got because of the order you created the files.  If it had been different, you would have gotten a different order of your results.

GLOB_BRACE is similar to GLOB_NOCASE above, in that it affects the interpretation of PATTERN, but it has the side effect of turning off the default use of GLOB_CSH causing the behavior to revert to ASCII sort.

I don't know if you can make any changes to the code of File::Glob without breaking backward compatibility.  I think a doc change is needed to clarify that:

Here's the crux of the issue:  With only one argument, the default is exactly as if GLOB_CSH was used.  This is mentioned under the bsd_glob() function description in the 5.16 and 5.18 documentation, but it is not mentioned in older versions of the documentation.  Especially, it is not mentioned in the 5.18 documentation, in the POSIX FLAGS section, that the flag defaults are *only* in effect if **the second argument is specified**.  The word "default" is used a couple of times in the POSIX FLAGS section, but those are only the defaults if the second argument is specified!  It took me a long time to figure that out.


As a side note, I wish there were a tag that would export bsd_glob() and the various constants, *without* overriding glob().  I'm not sure why overriding glob() is the default behavior for the :glob and :bsd_glob tags (except for historic precedent).  There doesn't seem to be any way to import the various constants without listing them individually or overriding glob().

Additionally, the :glob tag (:bsd_glob is not available in older perls) is discouraged, so a script with portability concerns has a choice between a "discouraged" tag with flaws, and an unavailable one.  And the performance of bsd_glob() is not really any better than writing your own function to return all the file names (as if "*" and ".*" had been specified, but filtering out "." and "..") and then filtering the returned names using grep() using regular expressions.

To top it off, the v5.8.8 version of bsd_glob is buggy (reported separately) when specifying the "universal" pattern:  bsd_glob("$dir/{.[!.],.??*,*}", GLOB_BRACE).  


The bottom line is that I've decided to avoid File::Glob (and glob(), etc) and use my own hand-rolled function, since I have to support code on perl 5.8.8.

I hope this is enough information to fix the File::Glob documentation.

Thanks for your work on perl!




>________________________________
> From: James E Keenan via RT <perlbug-followup@perl.org>
>To: 
>Cc: perl5-porters@perl.org 
>Sent: Saturday, September 14, 2013 12:45 PM
>Subject: [perl #119785] bsd_glob does not sort case-sensitively
> 
>
>Here's what I got running on Perl 5.18.0 and 'touch'-ing the same three
>files as you did:
>
>##########
>perl -MFile::Glob=:bsd_glob -le 'print for bsd_glob("*")'
>tt
>TTT
>uu
>
>perl -MFile::Glob=:bsd_glob -le 'print for bsd_glob("*",GLOB_ALPHASORT)'
>tt
>TTT
>uu
>
>perl -MFile::Glob=:bsd_glob -le 'print for bsd_glob("*",GLOB_CSH)'
>tt
>TTT
>uu
>
>perl -MFile::Glob=:bsd_glob -le 'print for bsd_glob("*",GLOB_NOSORT)'
>tt
>TTT
>uu
>
>perl -MFile::Glob=:bsd_glob -le 'print for bsd_glob("*",GLOB_NOCASE)'
>tt
>TTT
>uu
>
>perl -MFile::Glob=:bsd_glob -le 'print for bsd_glob("*",GLOB_BRACE)'
>TTT
>tt
>uu
>
>##########
>I think we should ask:
>
>1. Is this behavior expected and correct?
>
>2. If it is, what changes do we have to make in 'ext/File-Glob/Glob.pm'
>so that a user of File::Glob will have the correct expectations?
>
>Thank you very much.
>Jim Keenan
>
>---
>via perlbug:  queue: perl5 status: open
>https://rt.perl.org:443/rt3/Ticket/Display.html?id=119785
>
>
>
Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About