CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
A question about qr() operator

 



moroshko
Novice

Jul 10, 2008, 1:51 PM

Post #1 of 4 (605 views)
A question about qr() operator Can't Post

Hello Gurus !

Can someone tell me please why this program:


Code
   

#!/usr/bin/perl
use warnings;
use strict;

my $r = qr/^line2/;

my $s = <<STR;
line1
line2
STR

if ($s =~ /$r/m) {
print "Match";
} else {
print "No Match";
}



prints "No Match", while this program:


Code
   

#!/usr/bin/perl
use warnings;
use strict;

my $r = qr/^line2/m;

my $s = <<STR;
line1
line2
STR

if ($s =~ /$r/) {
print "Match";
} else {
print "No Match";
}



prints "Match" as desired ?

Why the first program doesn't match ??

Thanks in advance !


KevinR
Veteran


Jul 10, 2008, 4:46 PM

Post #2 of 4 (599 views)
Re: [moroshko] A question about qr() operator [In reply to] Can't Post

Your code prints "match" in both examples. I tested with activeperl 5.008.
-------------------------------------------------


moroshko
Novice

Jul 11, 2008, 2:00 AM

Post #3 of 4 (590 views)
Re: [KevinR] A question about qr() operator [In reply to] Can't Post

Strange...

I work with Perl 5.10.0.

Anybody else ?


KevinR
Veteran


Jul 11, 2008, 2:19 AM

Post #4 of 4 (589 views)
Re: [moroshko] A question about qr() operator [In reply to] Can't Post

it is strange because both regexps are the same. In one you turn on the "m" modifier in the saved regexp and in the other you just add it to the end of the regexp. They should both work the same as far as I know. If I remove the "m" completely then I get "No Match" as expected. Ask on www.perlmonks.com and post back here if you get an explanation.
-------------------------------------------------


(This post was edited by KevinR on Jul 11, 2008, 2:22 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives