Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^3: Pattern Matching

by AnomalousMonk (Archbishop)
on Mar 17, 2017 at 05:45 UTC ( [id://1184962]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Pattern Matching
in thread Pattern Matching

IIRC, this is from TheDamian's regex PBPs, which I religiously observe (the others, not so much). Things like  [(] [.] [ ] are visually useful — especially [ ], because what the heck does  \ mean anyway in  /x context, which we ought always to use?


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^4: Pattern Matching
by vrk (Chaplain) on Mar 17, 2017 at 10:20 UTC

    I recently decided to plunk

    use re '/x';

    in all my new code to make it the default. It takes a while to get used to the free space around tokens -- it's like antigolfing your regexes.

      As long as you're use-ing re '/flags' anyway, add  /ms to  /x to relieve that small, nagging, maddening ache in your left temporal region arising from always having to worry about just what  . ^ $ do, and the difference between single- and multi-line modes. (Another indisputable bit of wisdom from TheDamian.)


      Give a man a fish:  <%-{-{-{-<

        Indisputable, you say? Perhaps not.

        By avoiding use re I never have to worry about what . ^ $ do because they mean precisely what they have done for the last couple of decades, viz:

        • . matches any single character other than \n
        • ^ matches the start of the string
        • $ matches the end of the string

        Could not be simpler. If anyone should need to use /m or /s then they can tack that on to the regex as a modifier and anyone reading the code sees that it's clear that in this one case things might be different. (I've disregarded /x here as nobody ever needs that - it's just for syntactic clarity)

        However, if someone has done as you espouse then I might be entirely unaware of it because this declaration might occur nowhere near the regex I would currently be examining. A module with several thousand lines is (alas) not uncommon and a use re declaration on line 1000 is no use to me if I'm looking at line 4000 trying to work out why the hell the regex is misbehaving.

        It is my considered opinion therefore that use re /flags is likley to cause more problems than it solves with the notable exception of its use in a strictly limited lexical scope of a handful of lines or (at worst) a single self-contained subroutine. It's unlikely that I would ever use it even in that scenario, except perhaps to negate what some less charitable programmer might have set up more globally.

        Other opinions are available, of course, and there are no doubt various subjective reasons to choose one approach or the other. But your assertion that it is indisputable is unfounded.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-03-28 23:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found