more whitespace removal (from Michael G Schwern)
[p5sagit/p5-mst-13.2.git] / ext / File / Glob / Glob.pm
index c3b25fa..0e1382b 100644 (file)
@@ -2,24 +2,26 @@ package File::Glob;
 
 use strict;
 use Carp;
-use vars qw($VERSION @ISA @EXPORT_OK @EXPORT_FAIL %EXPORT_TAGS $AUTOLOAD);
+our($VERSION, @ISA, @EXPORT_OK, @EXPORT_FAIL, %EXPORT_TAGS,
+    $AUTOLOAD, $DEFAULT_FLAGS);
 
 require Exporter;
-require DynaLoader;
+use XSLoader ();
 require AutoLoader;
 
-@ISA = qw(Exporter DynaLoader AutoLoader);
+@ISA = qw(Exporter AutoLoader);
 
 @EXPORT_OK   = qw(
-    globally
     csh_glob
     glob
     GLOB_ABEND
     GLOB_ALTDIRFUNC
     GLOB_BRACE
+    GLOB_CSH
     GLOB_ERR
     GLOB_ERROR
     GLOB_MARK
+    GLOB_NOCASE
     GLOB_NOCHECK
     GLOB_NOMAGIC
     GLOB_NOSORT
@@ -28,16 +30,16 @@ require AutoLoader;
     GLOB_TILDE
 );
 
-@EXPORT_FAIL = ( 'globally' );
-
 %EXPORT_TAGS = (
     'glob' => [ qw(
         GLOB_ABEND
         GLOB_ALTDIRFUNC
         GLOB_BRACE
+        GLOB_CSH
         GLOB_ERR
         GLOB_ERROR
         GLOB_MARK
+        GLOB_NOCASE
         GLOB_NOCHECK
         GLOB_NOMAGIC
         GLOB_NOSORT
@@ -48,18 +50,24 @@ require AutoLoader;
     ) ],
 );
 
-$VERSION = '0.99';
-
-sub export_fail {
-    shift;
-
-    if ($_[0] eq 'globally') {
-        local $^W;
-        *CORE::GLOBAL::glob = \&File::Glob::csh_glob;
-        shift;
+$VERSION = '0.991';
+
+sub import {
+    my $i = 1;
+    while ($i < @_) {
+       if ($_[$i] =~ /^:(case|nocase|globally)$/) {
+           splice(@_, $i, 1);
+           $DEFAULT_FLAGS &= ~GLOB_NOCASE() if $1 eq 'case';
+           $DEFAULT_FLAGS |= GLOB_NOCASE() if $1 eq 'nocase';
+           if ($1 eq 'globally') {
+               local $^W;
+               *CORE::GLOBAL::glob = \&File::Glob::csh_glob;
+           }
+           next;
+       }
+       ++$i;
     }
-
-    @_;
+    goto &Exporter::import;
 }
 
 sub AUTOLOAD {
@@ -83,7 +91,7 @@ sub AUTOLOAD {
     goto &$AUTOLOAD;
 }
 
-bootstrap File::Glob $VERSION;
+XSLoader::load 'File::Glob', $VERSION;
 
 # Preloaded methods go here.
 
@@ -93,10 +101,17 @@ sub GLOB_ERROR {
 
 sub GLOB_CSH () { GLOB_BRACE() | GLOB_NOMAGIC() | GLOB_QUOTE() | GLOB_TILDE() }
 
+$DEFAULT_FLAGS = GLOB_CSH();
+if ($^O =~ /^(?:MSWin32|VMS|os2|dos|riscos|MacOS)$/) {
+    $DEFAULT_FLAGS |= GLOB_NOCASE();
+}
+
 # Autoload methods go after =cut, and are processed by the autosplit program.
 
 sub glob {
-    return doglob(@_);
+    my ($pat,$flags) = @_;
+    $flags = $DEFAULT_FLAGS if @_ < 2;
+    return doglob($pat,$flags);
 }
 
 ## borrowed heavily from gsar's File::DosGlob
@@ -127,10 +142,10 @@ sub csh_glob {
     # if we're just beginning, do it all first
     if ($iter{$cxix} == 0) {
        if (@pat) {
-           $entries{$cxix} = [ map { doglob($_, GLOB_CSH) } @pat ];
+           $entries{$cxix} = [ map { doglob($_, $DEFAULT_FLAGS) } @pat ];
        }
        else {
-           $entries{$cxix} = [ doglob($pat, GLOB_CSH) ];
+           $entries{$cxix} = [ doglob($pat, $DEFAULT_FLAGS) ];
        }
     }
 
@@ -168,8 +183,17 @@ File::Glob - Perl extension for BSD glob routine
     # an error occurred reading $homedir
   }
 
-  ## override the core glob (even with -T)
-  use File::Glob 'globally';
+  ## override the core glob (core glob() does this automatically
+  ## by default anyway, since v5.6.0)
+  use File::Glob ':globally';
+  my @sources = <*.{c,h,y}>
+
+  ## override the core glob, forcing case sensitivity
+  use File::Glob qw(:globally :case);
+  my @sources = <*.{c,h,y}>
+
+  ## override the core glob forcing case insensitivity
+  use File::Glob qw(:globally :nocase);
   my @sources = <*.{c,h,y}>
 
 =head1 DESCRIPTION
@@ -193,6 +217,11 @@ cannot open or read.  Ordinarily glob() continues to find matches.
 Each pathname that is a directory that matches the pattern has a slash
 appended.
 
+=item C<GLOB_NOCASE>
+
+By default, file names are assumed to be case sensitive; this flag
+makes glob() treat case differences as not significant.
+
 =item C<GLOB_NOCHECK>
 
 If the pattern does not match any pathname, then glob() returns a list
@@ -212,7 +241,7 @@ The FreeBSD extensions to the POSIX standard are the following flags:
 
 =item C<GLOB_BRACE>
 
-Pre-process the string to expand C<{pat,pat,...} strings like csh(1).
+Pre-process the string to expand C<{pat,pat,...}> strings like csh(1).
 The pattern '{}' is left unexpanded for historical reasons (and csh(1)
 does the same thing to ease typing of find(1) patterns).
 
@@ -228,6 +257,7 @@ behaviour and should probably not be used anywhere else.
 Use the backslash ('\') character for quoting: every occurrence of a
 backslash followed by a character in the pattern is replaced by that
 character, avoiding any special interpretation of the character.
+(But see below for exceptions on DOSISH systems).
 
 =item C<GLOB_TILDE>
 
@@ -288,10 +318,24 @@ that you can use a backslash to escape things.
 
 =item *
 
+On DOSISH systems, backslash is a valid directory separator character.
+In this case, use of backslash as a quoting character (via GLOB_QUOTE)
+interferes with the use of backslash as a directory separator. The
+best (simplest, most portable) solution is to use forward slashes for
+directory separators, and backslashes for quoting. However, this does
+not match "normal practice" on these systems. As a concession to user
+expectation, therefore, backslashes (under GLOB_QUOTE) only quote the
+glob metacharacters '[', ']', '{', '}', '-', '~', and backslash itself.
+All other backslashes are passed through unchanged.
+
+=item *
+
 Win32 users should use the real slash.  If you really want to use
 backslashes, consider using Sarathy's File::DosGlob, which comes with
 the standard Perl distribution.
 
+=back
+
 =head1 AUTHOR
 
 The Perl interface was written by Nathan Torkington E<lt>gnat@frii.comE<gt>,
@@ -302,7 +346,7 @@ following copyright:
 
     Copyright (c) 1989, 1993 The Regents of the University of California.
     All rights reserved.
-      
+
     This code is derived from software contributed to Berkeley by
     Guido van Rossum.