G
gargoyle
Consider the following code:
#!/usr/bin/perl -w
# NOTE: don't focus on the backslashes, I already know Win32 can
# use / instead. This is simply a regex+metachar question...
$old = 'C:\\test\\1\\'; # old parent dir
$new = 'C:\\test\\2\\'; # new parent dir
$path = 'C:\\test\\1\\bin\\'; # the path we're renaming
warn "old=$old, new=$new, path=$path\n";
warn "----- doing match! -----\n";
$path =~ /^(\Q$old\E)/; # this matches
warn "captured \$1 = $1\n";
$path =~ /^(\Q$new\E)/; # this doesn't
warn "captured \$1 = $1\n";
warn "old=$old, new=$new, path=$path\n";
warn "----- doing substitution! -----\n";
$path =~ s/^\Q$old\E/\Q$new\E/;
warn "old=$old, new=$new, path=$path\n";
__END__
The output:
old=C:\test\1\, new=C:\test\2\, path=C:\test\1\bin\
----- doing match! -----
captured $1 = C:\test\1\
captured $1 = C:\test\1\
old=C:\test\1\, new=C:\test\2\, path=C:\test\1\bin\
----- doing substitution! -----
old=C:\test\1\, new=C:\test\2\, path=C\:\\test\\2\\bin\
In the m// operation, the captured output is the original variable, not
the quotemeta version.
But in the s/// operation, the output is quoted.
Why are they behaving differently? And is there a way to get unquoted
output from the s/// operation, or am I just better off using substr(),
since after all there's no unquotemeta() function?
#!/usr/bin/perl -w
# NOTE: don't focus on the backslashes, I already know Win32 can
# use / instead. This is simply a regex+metachar question...
$old = 'C:\\test\\1\\'; # old parent dir
$new = 'C:\\test\\2\\'; # new parent dir
$path = 'C:\\test\\1\\bin\\'; # the path we're renaming
warn "old=$old, new=$new, path=$path\n";
warn "----- doing match! -----\n";
$path =~ /^(\Q$old\E)/; # this matches
warn "captured \$1 = $1\n";
$path =~ /^(\Q$new\E)/; # this doesn't
warn "captured \$1 = $1\n";
warn "old=$old, new=$new, path=$path\n";
warn "----- doing substitution! -----\n";
$path =~ s/^\Q$old\E/\Q$new\E/;
warn "old=$old, new=$new, path=$path\n";
__END__
The output:
old=C:\test\1\, new=C:\test\2\, path=C:\test\1\bin\
----- doing match! -----
captured $1 = C:\test\1\
captured $1 = C:\test\1\
old=C:\test\1\, new=C:\test\2\, path=C:\test\1\bin\
----- doing substitution! -----
old=C:\test\1\, new=C:\test\2\, path=C\:\\test\\2\\bin\
In the m// operation, the captured output is the original variable, not
the quotemeta version.
But in the s/// operation, the output is quoted.
Why are they behaving differently? And is there a way to get unquoted
output from the s/// operation, or am I just better off using substr(),
since after all there's no unquotemeta() function?