Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: example of 'm / / m' related example and compare to 'm / / s'

by TJPride (Pilgrim)
on Nov 28, 2011 at 18:55 UTC ( [id://940439]=note: print w/replies, xml ) Need Help??


in reply to PERL regex modifiers for m//

I often get confused as to which is which myself. I generally try both and then pick the one that works the way I want it to.
  • Comment on Re: example of 'm / / m' related example and compare to 'm / / s'

Replies are listed 'Best First'.
Re^2: example of 'm / / m' related example and compare to 'm / / s'
by AnomalousMonk (Archbishop) on Nov 29, 2011 at 08:53 UTC
    I often get confused as to which is which myself.

    This is the point of TheDamian's injunctions in Perl Best Practices (PBP) to always use the /m and /s (and /x) modifiers (BPs 148 and 151 – and 147) in every match and regex object definition:  . ^ $ always behave the same way and confusion is at least reduced if not eliminated.

      Hm. That's like advocating always taking a swimsuit & sunblock and a raincoat & umbrella cos it saves listening to the weather forecast. More than slightly ridiculous.

      The very reason it is hard, even for long-time Perlers with scads of frequent regex user miles, to remember which (/s /m) does what, is because they are so rarely required.

      So what could possibly be wrong with advocating their use at all times?

      For a start, you're crying wolf. By using them everywhere they become the norm, and after a while people stop asking themselves why is he using that here. And that is bad.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        Hm. That's like advocating always taking a swimsuit & sunblock and a raincoat & umbrella cos it saves listening to the weather forecast.

        /m and /s don't add functionality. They modify functionality.

        So using /ms is more like rewiring your oddly designed radio so that the tuning dial and the volume control actually work as you expect...thereby—for example—enabling you to successfully listen to weather forecasts.

        The very reason it is hard, even for long-time Perlers with scads of frequent regex user miles, to remember which (/s /m) does what, is because they are so rarely required.

        I'd argue that they are often required, just rarely used correctly.

        In my experience, matching start- and end-of-line is far more commonly needed that matching start- and end-of-string. The default behaviour is wrong practically every time anyone has to deal with multi-line data.

        Likewise, the vast majority of .* instances I see in deployed code are being used as "match anything", which they don't.

        BTW, it's easy to remember which is which: /s alters the behaviour of a single metacharacter (.) whereas /m alters the behaviour of multiple metacharacters (^ and $).

        By using them everywhere they become the norm

        Yes, that's precisely the point. The modified behaviours they provide should have been the norm from the start.

        after a while people stop asking themselves why is he using that here. And that is bad.

        Except that using them everywhere actually makes regexes work the way most people mistakenly think they already work. So even if they don't ask themselves why, they still get the "expected" behaviour.

        In other words, using /ms consistently on regexes makes the (idiosyncratic) behaviour of regexes conform to people's (reasonable) expectations, rather than vice versa. It's a simple technique that fixes an infelicity in Perl 5. And that's why PBP recommends it.

        Damian

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://940439]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (10)
As of 2024-04-18 09:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found