I would guess that the first one is intended to replace backslashes in a (DOS]Windows} filepath with forward slashes. What it actually says (or would say if it had an "s" before the first bar) is to replace all occurrences of two consecutive spaces with "space forward slash space'. (I assume you retyped these expressions and put spaces between each symbol? A backslash followed by a space refers to a literal space.) Even then, it still doesn't work because \ is a special symbol in regular expressions and needs to be "escaped" with a backslash in front of it. So it should be:
[code=perl]$path =~ s|\\|/|g;[/code]Furthermore, any paths with backslashes also have to have the backslashes escaped to be useful, e.g.:
c:\\foo\\bar\\b az.txt
(With only single backslashes as in "c:\foo\bar\baz .txt" the "path" would be interpreted as "c:[formfeed]oo[backspace, which erases the last "o"]ar[backspace again]az.txt". So:
The path starts out as 'c:
oaaz.txt'
which is probably not what you would want.)
The second also suffers from having spurious extra spaces between elements that cannot have them, as well as missing the "s". And in addition, the middle "/" is written as a "|". Also, the parentheses don't obviously contribute anything. Rewritten to make at least some sort of sense it might be:
[code=perl]$filename=~s/\s/\.\./;[/code]This line appears to take a filename containing spaces ( "\s" means a whitespace character, including space, tab, or newline.) and replaces the first of them with a pair of dots.
Try out this code
[code=perl]#! /usr/bin/perl
use strict;
my $path = "c:\\foo\\bar\\ baz.txt";
my $filename = "This is a filename with lots of spaces.doc";
print "The path starts out as \'$path\' \n";
$path =~ s|\\|/|g;
print "The path is now \'$path\' \n";
$filename=~s/\s/\.\./;
print "filename is \'$filename\' \n";[/code]Actually, you don't even need the backslashes in the replacement part of the RE. A dot is a special character in an RE and matches anything, so in the first part, you do need to write "\." in order to specify an actual dot. But in the second part, the RE engine is not trying to match anything and is just replacing the matched part of the string with a literal, so
[code=perl]$filename=~s/\s/../;[/code]works just as well. Try it.
If you wanted to replace all the spaces with dots (or underscores, which is more traditional) you could replace line 7 with:
[code=perl]$filename=~s/\s/_/g;[/code]
Best Regards,
Paul
Comment