Hello Monks,

Please see update below.

While doing pattern matches using qr// with modifiers added, I ran into behaviour I didn’t expect. The following code, using the 's' modifier to

“change "." to match any character whatsoever, even a newline, which normally it would not match.” (perlre: Modifiers)
illustrates what I mean:

#! perl use strict; use warnings; my $text = <<EOT; The quick brown fox jumps over the unfortunate dog. EOT my %searches = ( 'A' => 'fox.+?jumps', 'B' => '(?s)fox.+?jumps' ); my %functions = ( '1. no' => \&no_mod, '2. qr//' => \&qr_mod, '3. late' => \&late_mod, '4. inline' => \&inline_mod ); foreach my $search_key (sort keys %searches) { print "\nSearching on '$searches{$search_key}':\n\n"; foreach my $fn_key (sort keys %functions) { printf "%s%-9s 's' modifier: %s\n", $search_key, $fn_key, $functions{$fn_key}->($text, $searches{$search_key}) ? + 'Match found' : 'No match'; } } sub no_mod # no 's' modifier { my ($string, $pattern) = @_; my $regex = qr/$pattern/; return ($string =~ /$regex/); } sub qr_mod { my ($string, $pattern) = @_; my $regex = qr/$pattern/s; # <= 's' modifier return ($string =~ /$regex/); } sub late_mod { my ($string, $pattern) = @_; my $regex = qr/$pattern/; return ($string =~ /$regex/s); # <= 's' modifier } sub inline_mod { my ($string) = @_; return ($string =~ /fox.+?jumps/s); # <= 's' modifier } __END__

This is the output I get:

Searching on 'fox.+?jumps': A1. no 's' modifier: No match A2. qr// 's' modifier: Match found A3. late 's' modifier: No match A4. inline 's' modifier: Match found Searching on '(?s)fox.+?jumps': B1. no 's' modifier: Match found B2. qr// 's' modifier: Match found B3. late 's' modifier: Match found B4. inline 's' modifier: Match found

(I’m running DWIM/Strawberry perl 5.14.2 on Vista 32-bit, and I get the same result with perl 5.10.1 on Cygwin.)

All the results are as expected, except for A3. I can’t see any (logical) difference between the match patterns in late_mod() and inline_mod(), yet A4 matches (as expected) but A3 does not.

I’ve looked at perlop: Regexp Quote Like Operators, also perlfaq6: I'm having trouble matching over more than one line. What's wrong? and How do I match a regular expression that's in a variable? , , but I haven’t found anything that addresses this particular issue.

So my questions are:

  1. Is the match in A3 supposed to fail?
  2. If Yes, why?
  3. And then, why doesn’t the use warnings pragma result in a warning along the lines of: Useless use of pattern match modifier 's' on line 53 ?
  4. If No (i.e., the match should succeed), what am I doing wrong?

Update

Since posting, I’ve found How do I apply switches like /i or /g to a qr regexp? which addresses this issue. But I would still appreciate any further information or clarification.

Thanks,

Athanasius <°(((><contra mundum


In reply to Pattern matching with qr// and modifiers by Athanasius

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.