The syntax you use to embed the code is only valid in the "match" part of the substitution (left). To paste the code on the right side (which is just a normal double-quoted string of Perl), you can do this:
$file =~ s{<textarea rows="(.+?)"(.*?)>(.*?)</textarea>} {<textarea rows="@{[ length($3)/80 ]}"$2>$3</textarea>}gis;
This uses the Perl idiom "some string @{[ embedded_perl_code() ]} more string" .
But if you are working with a very complex operator, it may be easier to put the substitution in "eval" mode, where it treats the replacement string as Perl code:
$file =~ s{<textarea rows="(.+?)"(.*?)>(.*?)</textarea>} {'<textarea rows="' . (length($3)/80) . qq{"$2>$3</textarea>}}gise;
Note that in both examples, the regular expression is structured as s{}{} . This not only eliminates the need to avoid slashes, but also allows you to spread the expression across multiple lines for readability.
Eric Strom
source share