Do gube's original question and the fine answers above not point also to some optomization (a single instance in the symbol table of the two identical if...} clauses? Note that adding the third time thru, but using a variant if... (no 'g') produces a match there but then repeating the variant if...} finds no match in 4.
#!/usr/local/bin/perl
use strict;
use warnings;
my $regexp = 'Perl Monks';
my $regexp2 = 'Perl Monks is a wonderful place to visit'; # even if No
+de Reaper is sometimes a bit loud
if($regexp =~ m/^Perl Monks/gi) {
print "\nFound.0.";
} else {
print "\nNot Found.0.";
}
if($regexp =~ m/^Perl Monks/gi) {
print "\nFound.1.";
} else {
print "\nNot Found.1.";
}
if($regexp =~ m/^Perl Monks/i) { # No 'g'
print "\nFound.2.";
} else {
print "\nNot Found.2.";
}
if($regexp2 =~ m/^Perl Monks/gi) { # 'g' is back, but different $var
+being tested
print "\nFound.3.";
} else {
print "\nNot Found.3.";
}
if($regexp2 =~ m/^Perl Monks/gi) { # second identical use of $regex2
+and match
print "\nFound.4.";
} else {
print "\nNot Found.4.";
}
produces:
perl gube_g.pl
Found.0.
Not Found.1.
Found.2.
Found.3.
Not Found.4.
-
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.
|