DosGlob: Don’t parse an pattern that will not be used
authorFather Chrysostomos <sprout@cpan.org>
Sat, 29 Oct 2011 18:47:25 +0000 (11:47 -0700)
committerFather Chrysostomos <sprout@cpan.org>
Sat, 29 Oct 2011 18:47:25 +0000 (11:47 -0700)
This is the same change for efficiency’s sake that was applied to
File::Glob in commit edfed4c3099a.

lib/File/DosGlob.pm

index 4c4232c..23504ad 100644 (file)
@@ -115,20 +115,25 @@ sub glob {
     # glob without args defaults to $_
     $pat = $_ unless defined $pat;
 
-    # extract patterns
-    if ($pat =~ /\s/) {
+    # assume global context if not provided one
+    $cxix = '_G_' unless defined $cxix;
+
+    # if we're just beginning, do it all first
+    if (!$entries{$cxix}) {
+      # extract patterns
+      if ($pat =~ /\s/) {
        require Text::ParseWords;
        @pat = Text::ParseWords::parse_line('\s+',0,$pat);
-    }
-    else {
+      }
+      else {
        push @pat, $pat;
-    }
+      }
 
-    # Mike Mestnik: made to do abc{1,2,3} == abc1 abc2 abc3.
-    #   abc3 will be the original {3} (and drop the {}).
-    #   abc1 abc2 will be put in @appendpat.
-    # This was just the esiest way, not nearly the best.
-    REHASH: {
+      # Mike Mestnik: made to do abc{1,2,3} == abc1 abc2 abc3.
+      #   abc3 will be the original {3} (and drop the {}).
+      #   abc1 abc2 will be put in @appendpat.
+      # This was just the esiest way, not nearly the best.
+      REHASH: {
        my @appendpat = ();
        for (@pat) {
            # There must be a "," I.E. abc{efg} is not what we want.
@@ -162,19 +167,16 @@ sub glob {
            }
            goto REHASH;
        }
-    }
-    for ( @pat ) {
+      }
+      for ( @pat ) {
        s/\\{/{/g;
        s/\\}/}/g;
        s/\\,/,/g;
-    }
-    #print join ("\n", @pat). "\n";
+      }
+      #print join ("\n", @pat). "\n";
  
-    # assume global context if not provided one
-    $cxix = '_G_' unless defined $cxix;
-
-    # if we're just beginning, do it all first
-    $entries{$cxix} ||= [doglob(1,@pat)];
+      $entries{$cxix} = [doglob(1,@pat)];
+    }
 
     # chuck it all out, quick or slow
     if (wantarray) {