Bug in backslash-escapes in Dir.glob

I mentioned this problem before, now I have two test programs
that show it. One’s in Perl, one’s in Ruby. The Perl one behaves
correctly, the Ruby one not. First the Perl one, globtest.pl:

#! /usr/bin/perl -w
use strict;

my $testdir = shift @ARGV;

mkdir($testdir);

open(FILE, “>$testdir/file 1”);
close(FILE);
open(FILE, “>$testdir/file 2”);
close(FILE);

$testdir =~ s/ /?/g;
my @files = glob("$testdir/*\ *");

for my $file (@files)
{
print “$file\n”;
}

$ perl globtest.pl 'glob test’
glob test/file 1
glob test/file 2
$

So far so good. Now the ruby version, globtest.rb:

#! /usr/bin/ruby

testdir = ARGV.shift
Dir.mkdir(testdir)

(1…2).each { |i|
File.new("#{testdir}/file #{i}", “w”).close
}

p Dir.glob("#{testdir.gsub(/ /, ‘?’)}/*\ *")

$ ruby globtest.rb ‘glob test’
[“glob test/file 1”, “glob test/file 2”, “globtest.rb”, “glob test”]
$

The problem is that no amount of backslash-escaping of the space in the
glob pattern prevents the closing “*” from matching everything.

Clifford Heath.

Hi,

p Dir.glob(“#{testdir.gsub(/ /, ‘?’)}/*\ *”)

The last backslash has no meanings at all. Just a literal
space.

$ ruby globtest.rb ‘glob test’
[“glob test/file 1”, “glob test/file 2”, “globtest.rb”, “glob test”]

In 1.6, spaces are separators of patterns, so you are globbing
“glob?test/" and "” here.

···

At Wed, 14 May 2003 10:59:18 +0900, Clifford Heath wrote:


Nobu Nakada

The last backslash has no meanings at all. Just a literal space.

Yes. But use two backslashes, or four, six or eight, and you get
exactly the same result.

In 1.6, spaces are separators of patterns, so you are globbing
“glob?test/" and "” here.

Are you saying that bug is fixed in a later version?

···

nobu.nokada@softhome.net wrote:

Hi,

The last backslash has no meanings at all. Just a literal space.

Yes. But use two backslashes, or four, six or eight, and you get
exactly the same result.

Escaping meta-character is also 1.7 feature, it doesn’t work in
1.6. So it was impossible to contain spaces in glob patterns
as matching characters.

In 1.6, spaces are separators of patterns, so you are globbing
“glob?test/" and "” here.

Are you saying that bug is fixed in a later version?

It was a feature modified in 1.7, spaces have no longer special
meanings now. You have to use ‘\0’, which is available even in
1.6, to separate patterns instead.

···

At Wed, 14 May 2003 15:39:48 +0900, Clifford Heath wrote:


Nobu Nakada