Joe Orton wrote:
> Does the quote escaping really work in this function?
hmm, it doesn't look like it does. I think this was part of some work that
stas and ken were doing, though, and IIRC it was some win32 command line
thing. but I could be wrong.
> It confuses emacs
> font-lock mode which doesn't see a closing quote so thinks the rest of
> the file is part of the string. This fixes at least the latter:
> - $arg =~ s/"/\"/g;
> + $arg =~ s/"/\\"/g;
that looks better, but I'm not sure it's right either - if the input is
already escaped then you've (both) over-quoted (if the first worked at all,
that is :) so, you look to be fine here if pre-quoted input is forbidden or
not expected, but not so if it's already quoted, in which case something
like this is better:
$arg =~ s!([^\\])"!$1\\"!g;
the attached script runs through a few different regex and input
combinations for visual inspection. hopefully that will help sort this
issue out a bit (or the original author will clear things up ;)
--Geoff
#!/usr/bin/perl
foreach my $data (<DATA>) {
chomp $data;
print 'old: ', shell_ready_old($data), "\n";
print 'joe: ', shell_ready_joe($data), "\n";
print 'geoff: ', shell_ready_geoff($data), "\n";
print '-' x 30, "\n";
}
sub shell_ready_old {
my $arg = shift;
$arg =~ s/"/\"/g;
return qq["$arg"];
}
sub shell_ready_joe {
my $arg = shift;
$arg =~ s/"/\\"/g;
return qq["$arg"];
}
sub shell_ready_geoff {
my $arg = shift;
$arg =~ s!([^\\])"!$1\\"!g;
return qq["$arg"];
}
__DATA__
one "fish" two "fish"
red \"fish\" blue \"fish\"
this one has "a little star
this one has a \"little car
say, \"what \"a \"lot of fish there are