It's unfortunate that Perl doesn't know to optimize the following automatically :($doc =~ s/(?:\[|\]|\')//g --> $doc =~ s/[\[\]\']//g
$doc =~ s/(?:\[|\]|\')+//g --> $doc =~ s/[\[\]\']+//g
Maybe unfortunate in your specific example, but really not surprising at all. The statements (whilst the same in this very specific instance) are not actually the same at all. Consider:
use 5.16.2;
use warnings;
my $doc = "'C-3PO' or 'See-Threepio' is a humanoid robot character fro
+m the [[Star Wars]] universe who appears in the original ''Star Wars'
+' films, the prequel trilogy and the sequel trilogy.\n";
$doc =~ s/(?:\[\[|\]\]|'')//g; # Only replace doubles, to make sequenc
+e longer than a single character
say $doc;
$doc =~ "'C-3PO' or 'See-Threepio' is a humanoid robot character from
+the [[Star Wars]] universe who appears in the original ''Star Wars''
+films, the prequel trilogy and the sequel trilogy.\n";
$doc =~ s/[\[\]']//g; # set based replacement
say $doc;
In English, the first match could be described as Match sequence x or sequence y or sequence z, do not keep the matching group. The second match could be described as Match any characters in set a.
Sometimes there is a trade-off in just how much work the optimiser will do. Obvious conversions due to simple style differences are easy and cheap. Less obvious conversions like this, where in some edge cases it is faster to optimise, I suspect you'll find the decision is to leave the optimisation up to the developer more often than not.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|