sciguy has asked for the wisdom of the Perl Monks concerning the following question:
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";
I even went so far as to tabulate the results (by inspection). A "/" means "\n":(?^: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
/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 | |
by Anonymous Monk on Dec 31, 2014 at 04:24 UTC | |
by Anonymous Monk on Dec 31, 2014 at 04:56 UTC | |
by Anonymous Monk on Dec 31, 2014 at 04:25 UTC | |
by LanX (Saint) on Dec 31, 2014 at 04:54 UTC | |
by AnomalousMonk (Archbishop) on Dec 31, 2014 at 18:29 UTC | |
|
Re: /s and /m don't seem to be doing anything
by LanX (Saint) on Dec 31, 2014 at 03:34 UTC | |
by Anonymous Monk on Dec 31, 2014 at 04:06 UTC | |
by LanX (Saint) on Dec 31, 2014 at 04:45 UTC | |
|
Re: /s and /m don't seem to be doing anything (perlre)
by Anonymous Monk on Dec 31, 2014 at 03:23 UTC |