That's because of the /g switch in the first match.From perlop:
The "/g" modifier specifies global pattern matching--that is,
matching as many times as possible within the string. How it
behaves depends on the context. In list context, it returns a
list of the substrings matched by any capturing parentheses in
the regular expression. If there are no parentheses, it returns
a list of all the matched strings, as if there were parentheses
around the whole pattern.
In scalar context, each execution of "m//g" finds the next match,
returning true if it matches, and false if there is no further
match. The position after the last match can be read or set
using the pos() function; see "pos" in perlfunc. A failed match
normally resets the search position to the beginning of the
string, but you can avoid that by adding the "/c" modifier (e.g.
"m//gc"). Modifying the target string also resets the search
position.
Aha. In scalar context (boolean is scalar, and you are using the match in an if-expression)
the /g matching returns the next match. So the match-operator keeps its state with
regard to that match, and repeating the same match returns again the next match - and there is no more match. Failing the match the match-opreator gets reset. Consider:
use strict;
use warnings;
my $regexp = 'Perl Monks';
if($regexp =~ m/^Perl Monks/gi) {
print "\nFound..";
} else {
print "\nNot Found..";
}
if($regexp =~ m/^Perl Monks/gi) {
print "\nFound.1.";
} else {
print "\nNot Found.1.";
}
if($regexp =~ m/^Perl Monks/gi) {
print "\nFound.2.";
} else {
print "\nNot Found.2.";
}
print "\n";
__END__
Found..
Not Found.1.
Found.2.
--shmem
_($_=" "x(1<<5)."?\n".q·/)Oo. G°\ /
/\_¯/(q /
---------------------------- \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
-
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.
|