develooper Front page | perl.perl5.porters | Postings from July 2008

[PATCH] Enable File::DosGlob to handle arbitray glob patterns

From:
alex.davies
Date:
July 27, 2008 10:39
Subject:
[PATCH] Enable File::DosGlob to handle arbitray glob patterns
Message ID:
8359169A932A4EB99BE51505C8530D41@Amelie

Here's a patch that simplifies passing arbitrary glob
patterns to C< File::DosGlob::glob() > by making it accept an
array reference containing the glob patterns.

Rationale - if you want to DosGlob a path containing both
spaces and backslashes eg.

    C:\Program Files\*

You need to 'escape' it such that it passes thro'
C<Text::ParseWords::parse_line()> in one piece. I currently
use this to cover globbing arbitrary paths:

    sub dosglob {
        require File::DosGlob;
        my $pat = $_[0]; # just handle one pattern
        if ($pat =~ /[ ']/) {
            # escape it such that Text::ParseWords::parse_line()
            #  sees unquoted text
            $pat =~ s|\\|\\\\|g;
            $pat =~ s|([ '])|\\$1|g;
        }
        return File::DosGlob::glob($pat);
    };

..but it seems like overkill.

My proposed change would allow you to simply say:

    File::DosGlob::glob \@glob_patterns
    File::DosGlob::glob ["C:\\Program Files\\*"]

Cheers, alex.

# %<

--- DosGlob.pm-orig 2008-07-27 17:01:27.140625000 +0100
+++ DosGlob.pm-new 2008-07-27 17:13:36.593750000 +0100
@@ -9,7 +9,7 @@
 
 package File::DosGlob;
 
-our $VERSION = '1.01';
+our $VERSION = '1.02';
 use strict;
 use warnings;
 
@@ -313,7 +313,10 @@
     $pat = $_ unless defined $pat;
 
     # extract patterns
-    if ($pat =~ /\s/) {
+    if (ref($pat) eq 'ARRAY') {
+ @pat = @$pat;
+    }
+    elsif ($pat =~ /\s/) {
  require Text::ParseWords;
  @pat = Text::ParseWords::parse_line('\s+',0,$pat);
     }
@@ -434,6 +437,9 @@
     @perlfiles = glob  "..\\pe?l/*.p?";
     print <..\\pe?l/*.p?>;
 
+    # to glob arbitrary pathnames, pass an array ref:
+    @matches = glob ["C:\\Program Files\\*\\*"];
+
     # from the command line (overrides only in main::)
     > perl -MFile::DosGlob=glob -e "print <../pe*/*p?>"
 
@@ -460,6 +466,10 @@
 C<Text::ParseWords::parse_line()>, so see L<Text::ParseWords> for details
 of the quoting rules used.
 
+From version 1.02 onwards, C<File::DosGlob::glob()> also accepts an array
+reference containing the glob patterns. This allows you to pass arbitrary
+patterns which might contain both spaces and backslashes.
+
 Extending it to csh patterns is left as an exercise to the reader.
 
 =head1 NOTES







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