/e
RHS means Right Hand Side. Suppose we have an HTML file, which contains:
<FONT SIZE=2> <FONT SIZE=4> <FONT SIZE=6>
and we wish to double the size of each font so 2 becomes 4 and 4 becomes 8 etc. What about :
$data="<FONT SIZE=2> <FONT SIZE=4> <FONT SIZE=6>"; print "$data\n"; $data=~s/(size=)(\d)/\1\2 * 2/ig; print "$data\n";
which doesn't really work out. What this does is match
size=x, where x is any digit. The first match,
size=, goes into $1 and
the second match, whatever the digit is, goes into $2 . The second part of the regex simply
prints $1 and $2 (referred to as \1 and \2 ), and attempts to multiply $2
by 2. Remember /i means
case insensitive matching.
What we need to do is evaluate the right hand side of the regex as an expression - that is not just print out what it says, but actually evaluate it. That means work it through, not blindly treat it as string. Perl can do this:
$data=~s/(size=)(\d)/$1.($2 * 2)/eig;
A little explanation....the LHS is the same as before. We add
/e so Perl evaluates the RHS as an
expression. So we need to change \1
into $1 and so on. The
parens are there to ensure that $2 * 2
is evaluated, then joined to $1 . And that's it !