[sf-perl] matching digits in braces in globs

David Alban extasia at extasia.org
Tue Jun 17 19:34:50 PDT 2008


greetings,

i'd like to use File::Glob::bsd_glob() to match paths which may or may
not end in the regular expression:

   m{ - \d{3} }x

i'm in the classic situation where yes, tmtowtdi, and i can easily
find workarounds, but i'd like to know why what i'm trying doesn't
work.  here's some example code that shows what i'm experiencing:



use Data::Dumper;
use File::Glob qw( :glob );

print "files:\n";
system "ls /tmp/tmp2";

print "\nglob #1, matching with bsd_glob (fails):\n";
$glob = '/tmp/tmp2/foo{-[0-9][0-9][0-9],}';
print Dumper $glob;
print Dumper [ bsd_glob( $glob, GLOB_BRACE ) ];

print "\nglob #1, matching with core glob (fails):\n";
print Dumper  '/tmp/tmp2/foo{-[0-9][0-9][0-9],}';
my @matches = </tmp/tmp2/foo{-[0-9][0-9][0-9],}>;
print Dumper \@matches;

print "\nglob #1, matching with bash (succeeds):\n";
print Dumper $glob;
system qq{ ls -l $glob };

print "\nglob #2, matching with bsd_glob (not matching digits inside
braces, so succeeds):\n";
my $glob = '/tmp/tmp2/foo{-???,}';
print Dumper $glob;
print Dumper [ bsd_glob( $glob, GLOB_BRACE ) ];

print "\nglob #3, matching with bsd_glob (not matching digits inside
braces, so succeeds):\n";
$glob = '/tmp/tmp2/foo[0-9][0-9][0-9]';
print Dumper $glob;
print Dumper [ bsd_glob( $glob, GLOB_BRACE ) ];




here's the output:



files:
foo  foo001  foo-001  foo.bar  junk.perl

glob #1, matching with bsd_glob (fails):
$VAR1 = '/tmp/tmp2/foo{-[0-9][0-9][0-9],}';
$VAR1 = [
          '/tmp/tmp2/foo'
        ];

glob #1, matching with core glob (fails):
$VAR1 = '/tmp/tmp2/foo{-[0-9][0-9][0-9],}';
$VAR1 = [
          '/tmp/tmp2/foo'
        ];

glob #1, matching with bash (succeeds):
$VAR1 = '/tmp/tmp2/foo{-[0-9][0-9][0-9],}';
-rw-r--r--  1 root root 0 Jun 18 01:49 /tmp/tmp2/foo
-rw-r--r--  1 root root 0 Jun 18 01:49 /tmp/tmp2/foo-001

glob #2, matching with bsd_glob (not matching digits inside braces, so
succeeds):
$VAR1 = '/tmp/tmp2/foo{-???,}';
$VAR1 = [
          '/tmp/tmp2/foo-001',
          '/tmp/tmp2/foo'
        ];

glob #3, matching with bsd_glob (not matching digits inside braces, so
succeeds):
$VAR1 = '/tmp/tmp2/foo[0-9][0-9][0-9]';
$VAR1 = [
          '/tmp/tmp2/foo001'
        ];



can anyone tell me why i can't match '-[0-9][0-9][0-9]' inside the braces?

i did find the following in the bash man page:

       Brace expansion is performed before any other expansions, and any char-
       acters  special to other expansions are preserved in the result.  It is
       strictly textual.  Bash does not apply any syntactic interpretation  to
       the context of the expansion or the text between the braces.

even if bsd_glob behaves like bash, that doesn't explain it.  it
simply means that the '[0-9]' are preserved for a subsequent
expansion, and indeed, bash does finally expand and match these
constructs.

thanks,
david
-- 
Live in a world of your own, but always welcome visitors.


More information about the SanFrancisco-pm mailing list