You can use any non-alphanumeric character as the
separator in a s/// or m// operator. This allows you to
choose a separator which doesn't clash with the data in the
string.
In this case, if you used the 'traditional' slash as the
delimiter then you'd need to escape the slash that appears
in the regex. By changing the separator, you don't need
to do that.
You can also use 'bracketing' characters, like (,
[, <
or {. In that case you need to use the opposite character
at the other end, like this:
s[something][else];
This is all explained in perldoc perlop.
--
<http://www.dave.org.uk>
"Perl makes the fun jobs fun
and the boring jobs bearable" - me
| [reply] [d/l] |
What perlop doesn't tell you is that, if you put a space after the operator name, you can use alphanumeric characters as delimiters. To use the current example:
($Z = $0) =~ s z.*/zz;
I also made use of this in homer.pl, with m mmm, Donuts. But please don't do this in production code! :)
| [reply] [d/l] [select] |
| [reply] [d/l] [select] |
Thank you very much.
(Alas, perhaps I count from 1 to 10 too quickly :) )
| [reply] |
$z= $0; # store the name of the script in $z
$z=~ s{.* # anything, greedy match
/ # then a / (the last one in $z as the match is greedy)
}{}x; # removed
So this snippet just returns the basename of the script.
And as you see you can also use brackets like { } as
delimiters, and the x modifier lets you comment the regexp. | [reply] [d/l] |
There is a real good reason why there is an option for
changing the delimiter in pattern substitution. Witness
the following code I used to alter URLS that needed to
appear in a text field in a flatfile database:
while(<>){ #take input from STDIN
s/\/\//\\\/\\\//g;
print STDOUT;
}
Why? Because the designers of this flatfile database decreed
that "//" would be interpreted as the beginning of a
comment, thus causing everything after "http://" to be
lost. So I escaped those particular forward slashes using s///;
It looks atrocious, but it did the job.
Using an alternate delimiter wouldn't eliminate the need
for all the forward and back slashes, but at least it
would show me (or another trying to decipher the above
atrocity) where the expression was delimited.
-----
"Computeri non cogitant, ergo non sunt" | [reply] [d/l] |