http://qs321.pair.com?node_id=788519

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

Dear all,
Can any one explain me about "m" and "s" modifier with an example
I referred perlretut but I was not able to understand that
Please help me out

Replies are listed 'Best First'.
Re: m modifier
by ELISHEVA (Prior) on Aug 14, 2009 at 06:02 UTC

    A vast improvement over your last question! (saying what you tried)

    You are right - perlretut only tells you that there is an m modifier. It doesn't really explain it. For an explanation, look at perlre

    Best, beth

Re: m modifier
by hnd (Scribe) on Aug 14, 2009 at 06:13 UTC
    i had this doubt before and got it cleared here.... this is what i learned....

    /m modifier are used to to let $ and ^ match before and after a \n respectively for eg if you do  /^foo/m it would not only match at the beginning but also right after a newline character (ie the second line)...

    now for /s modifier it is necessary to know what '.' does usually. it doesn't match the newlines normally ie you have to force it to match the newlines as well for eg  /foo.bar/s would match foo and bar on different lines...

    =====================================================
    i'am worst at what do best and for this gift i fell blessed...
    i found it hard it's hard to find well whatever
    NEVERMIND
Re: m modifier
by perldesire (Scribe) on Aug 14, 2009 at 12:40 UTC

    m modifier treats the string as a set of multi line. In your regular expression "." (dot) matches a character other than \n (newline), because it treats the string as a multiline.

    s modifier is very simple, it takes the whole string (doesnt care about how many \n), and consider as a single long string.

    my $string; $string="PERL is an Acronym of practical extraction and report languag +e\ni like it very much"; # here "." is as usual i.e any single character.(including \n) so it m +atches and returns 1. print $string =~ /language.i/s; # here "." matches any character other than \n, but in our string lang +uage followed by \n is there. so it doesnt return 1. its false. print $string =~ /language.i/m; # here \n followed by i will match so returns true. print $string =~ /language\ni/m; # As simple s modifier consider . as a single character. that could be + anything... returns true... print $string =~ /language\ni/s;