sciguy has asked for the wisdom of the Perl Monks concerning the following question:

Hello, Perl Monks!

I have been having problems with string searches. I learned from a Perl Monks user that /m or /s were to be replacements for the deprecated "$*" which I am encountering in some old code that I didn't write, but need to modify to a newer system (using perl 5.14.2).

The script I am editing has the statement "$* = 0;". $* is never used again in the script. I read elsewhere that if I commented that statement out in the perl versions where it was deprecated, nothing would happen. I don't feel sure about that.

The docs were not very satisfying, so I decided to play around with various situations regarding pattern matching using /m, /s or just plain / to see what happens. Here is a script I wrote:

#! /usr/bin/perl -w $re=qr/foo$/; print $re; print "\n"; print "foo\\nbar\\n =~ \/$re\/s \t"; print "foo\nbar\n" =~ /$re/s ?"true":"false"; print "\n"; print "foo\\nbar\\n =~ \/$re\/m \t"; print "foo\nbar\n" =~ /$re/m ?"true":"false"; print "\n"; # print "fred\\nfoo\\nbar\\n =~ \/$re\/s \t"; print "fred\nfoo\nbar\n" =~ /$re/s ?"true":"false"; print "\n"; print "fred\\nfoo\\nbar\\n =~ \/$re\/m \t"; print "fred\nfoo\nbar\n" =~ /$re/m ?"true":"false"; print "\n"; # print "foo =~ \/$re\/s \t"; print "foo" =~ /$re/s ?"true":"false"; print "\n"; print "foo =~ \/$re\/m \t"; print "foo" =~ /$re/m ?"true":"false"; print "\n"; print "\\nfoo =~ \/$re\/s \t"; print "\nfoo" =~ /$re/s ?"true":"false"; print "\n"; print "\\nfoo =~ \/$re\/m \t"; print "\nfoo" =~ /$re/m ?"true":"false"; print "\n"; print "foobar =~ \/$re\/s \t"; print "foobar" =~ /$re/s ?"true":"false"; print "\n"; print "foobar =~ \/$re\/m \t"; print "foobar" =~ /$re/m ?"true":"false"; print "\n"; print "bar\\nfoo\\n =~ \/$re\/s \t"; print "bar\nfoo\n" =~ /$re/s ?"true":"false"; print "\n"; print "bar\\nfoo =~ \/$re\/m \t"; print "bar\nfoo" =~ /$re/m ?"true":"false"; print "\n"; print "bar\\nfoo\\n =~ \/$re\/ \t"; print "bar\nfoo\n" =~ /$re/ ?"true":"false"; print "\n"; print "foo\\nbar\\n =~ \/$re\/ \t"; print "foo\nbar\n" =~ /$re/ ?"true":"false"; print "\n"; print "fred\\nfoo\\nbar\\n =~ \/$re\/ \t"; print "fred\nfoo\nbar\n" =~ /$re/ ?"true":"false"; print "\n"; print "fred\\nfoo\\nbar\\n =~ \/$re\/ \t"; print "fred\nfoo\nbar\n" =~ /$re/ ?"true":"false"; print "\n"; print "foo =~ \/$re\/ \t"; print "foo" =~ /$re/ ?"true":"false"; print "\n"; print "foobar =~ \/$re\/ \t"; print "foobar" =~ /$re/ ?"true":"false"; print "\n"; print "\\nfoo =~ \/$re\/ \t"; print "\nfoo" =~ /$re/ ?"true":"false"; print "\n";
The output was:
(?^:foo$) foo\nbar\n =~ /(?^:foo$)/s false foo\nbar\n =~ /(?^:foo$)/m false fred\nfoo\nbar\n =~ /(?^:foo$)/s false fred\nfoo\nbar\n =~ /(?^:foo$)/m false foo =~ /(?^:foo$)/s true foo =~ /(?^:foo$)/m true \nfoo =~ /(?^:foo$)/s true \nfoo =~ /(?^:foo$)/m true foobar =~ /(?^:foo$)/s false foobar =~ /(?^:foo$)/m false bar\nfoo\n =~ /(?^:foo$)/s true bar\nfoo =~ /(?^:foo$)/m true bar\nfoo\n =~ /(?^:foo$)/ true foo\nbar\n =~ /(?^:foo$)/ false fred\nfoo\nbar\n =~ /(?^:foo$)/ false fred\nfoo\nbar\n =~ /(?^:foo$)/ false foo =~ /(?^:foo$)/ true foobar =~ /(?^:foo$)/ false \nfoo =~ /(?^:foo$)/ true
I even went so far as to tabulate the results (by inspection). A "/" means "\n":
/m /s none foo/bar/ F F F fred/foo/bar F F F foo T T T /foo T T T foobar F F F bar/foo/ T T T bar/foo T T T

In none of these situations did /m or /s produce a different result from each other. And in all cases, just having / with nothing after it produced the same results as the other two with options after them. I am not clear at all about the purpose of /m and /s (or the original purpose of $*, for that matter).

If anyone can clear this up, or can suggest situations I didn't test, I would be grateful.

sciguy

Replies are listed 'Best First'.
Re: /s and /m don't seem to be doing anything
by Anonymous Monk on Dec 31, 2014 at 03:51 UTC

    Don't forget the basic debugging step of printing your things to check if they are what you think they are:

    my $re1=qr/foo$/; my $re2=qr/foo$/m; my $re3=qr/foo$/s; $,="\t"; $\="\n"; # just for output print qr/$re1/, qr/$re1/m, qr/$re1/s; print qr/$re2/, qr/$re2/m, qr/$re2/s; print qr/$re3/, qr/$re3/m, qr/$re3/s; __END__ (?^:foo$) (?^:foo$) (?^:foo$) (?^m:foo$) (?^m:foo$) (?^m:foo$) (?^s:foo$) (?^s:foo$) (?^s:foo$)

    So that means...

    my $re1=qr/foo$/; print $re1, "\n"; print "foo\\nbar\\n =~ \/$re1\/ \t"; print "foo\nbar\n" =~ /$re1/ ?"true":"false"; print "\n"; print "foo\\nbar\\n =~ \/$re1\/m \t"; print "foo\nbar\n" =~ /$re1/m ?"true":"false"; print "\n"; my $re2=qr/foo$/m; print $re2, "\n"; print "foo\\nbar\\n =~ \/$re2\/ \t"; print "foo\nbar\n" =~ /$re2/ ?"true":"false"; print "\n"; print "foo\\nbar\\n =~ \/$re2\/m \t"; print "foo\nbar\n" =~ /$re2/m ?"true":"false"; print "\n"; __END__ (?^:foo$) foo\nbar\n =~ /(?^:foo$)/ false foo\nbar\n =~ /(?^:foo$)/m false (?^m:foo$) foo\nbar\n =~ /(?^m:foo$)/ true foo\nbar\n =~ /(?^m:foo$)/m true

    (I haven't yet found the relevant bit of documentation that makes this behavior clear.)

      Combining this with LanX's comment about metachars gives enlightenment:

      my $re1=qr/foo$/; my $re2=qr/foo$/m; $,="\t"; $\="\n"; # just for output print tf(qr/$re1/), tf(qr/$re1/m); print tf(qr/$re1$/), tf(qr/$re1$/m); print tf(qr/$re2/), tf(qr/$re2/m); print tf(qr/$re2$/), tf(qr/$re2$/m); sub tf { "$_[0] ".("foo\nbar\n"=~$_[0]?"T":"F") } __END__ (?^:foo$) F (?^:foo$) F (?^:(?^:foo$)$) F (?^m:(?^:foo$)$) F (?^m:foo$) T (?^m:foo$) T (?^:(?^m:foo$)$) F (?^m:(?^m:foo$)$) T

      i.e. the modifiers need to be on the regex which contains the metacharacters which they affect. Makes sense but I didn't catch it right away.

      perlop says this:

      If a precompiled pattern is embedded in a larger pattern then the effect of "msixpluad" will be propagated appropriately.

      It's possible I've missed some other documentation, but I find this statement possibly a bit misleading, since "appropriately" could be taken to mean that, for example, qr/$re1/m should propagate /m into $re1, as the OP expected.

        I can't test but I'm sure including $re as string w/o precompiling doesn't preserve any flags.

        Cheers Rolf

        (addicted to the Perl Programming Language and ☆☆☆☆ :)

Re: /s and /m don't seem to be doing anything
by LanX (Saint) on Dec 31, 2014 at 03:34 UTC
    None of your tests included any of the .$^ meta characters, so how do you want to see any effect?

    See perlre

    According to the 5.8 docs $*=0 is just the default without /m or /s. Someone was just playing safe...

    So no need to migrate! :)

    HTH!

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)

      None of your tests included any of the .$^ meta characters, so how do you want to see any effect?

      So LanX, what is all this  /(?^:foo$)/s in OPs post?

        I checked the code and true there is one $ in line 4 which I oversaw.

        Mea culpa!

        Cheers Rolf

        (addicted to the Perl Programming Language and ☆☆☆☆ :)

Re: /s and /m don't seem to be doing anything (perlre)
by Anonymous Monk on Dec 31, 2014 at 03:23 UTC

    ... . I am not clear at all about the purpose of /m and /s (or the original purpose of $*, for that matter). If anyone can clear this up, I would be grateful. sciguy

    see perlre and perlrequick and perlrebackslash and see wxPPIxregexplain.pl/ ppixregexplain.pl

    it says stuff like

    . # any character (including \n) alias [\w\W] alias [\s\ +S] alias [\d\D] alias \p{All} # (/m) with ^ and $ matching start and end of line # (/s) with . matching \n # any character (including \n) alias [\w\W] alias [\s\S] +alias [\d\D] alias \p{All} # address=/15/C1/C0 # xRe::Token::Assertion # simple zero-width assertion (zero-length, between pos() +itions) # match the beginning of the string "^", # address=/15/C1/C2 # xRe::Token::Assertion # simple zero-width assertion (zero-length, between pos() +itions) # match before an optional \n, and the end of the string "\$", # xRe::Token::Assertion # simple zero-width assertion (zero-length, between pos() +itions) # (?m) with ^ and $ matching start and end of line # match the beginning of a "line" "^", # address=/16/C1/C2 # xRe::Token::Assertion # simple zero-width assertion (zero-length, between pos() +itions) # (?m) with ^ and $ matching start and end of line # match before an optional \n, and the end of a "line" "\$",