Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re^2: Best practice or cargo cult?

by sfink (Deacon)
on Jun 22, 2006 at 05:47 UTC ( #556839=note: print w/replies, xml ) Need Help??

in reply to Re: Best practice or cargo cult?
in thread Best practice or cargo cult?

Thank you. I haven't read the book, which prevents me from disagreeing with its conclusions without being an ass.

I'm going to disagree now, but I still haven't read the book. I'm relying on your interpretation. So perhaps that's worth half.

My half-assed opinion: /m is usually irrelevant to what people expect. The vast majority of programs end up reading things line-by-line. If you're only looking at a line at a time, then /m has no effect.

Therefore, it is only relevant in the less-common circumstances when you have embedded newlines. I find that in those cases, I sometimes mean one thing, sometimes the other. As a result, I find /m sometimes useful, and sometimes it does the opposite of what I want. So I use it when it is what I need, and when reading such code, the existence of that option is a very useful indicator of what the expression is doing. Using it all the time, even when it has no effect, harms readability.

As for /s, consider this common idiom:

while (<>) { if (/^title:\s*(.*)/i) { $title = $1; } }
What does that do? Well, it looks at a config file for a line saying "Title: The Adventures of the Were-Gerbil" and pulls out the title. If that included the newline, I would be rather annoyed. It's like asking someone "what's in the bag?" and receiving the answer "an apple, a sandwich, and a bunch of air". Smartass.

Also, similarly to /m, I use /s specifically in situations where I want to do something unexpected with respect to newlines. I prefer using it sparingly, so that those situations are more noticeable. Most of the time, I'd rather use something like [\w\W] rather than a trailing /s, especially when what I really want is a mixture of both possible meanings of '.'.

Always using /x, on the other hand, makes sense to me, even though I haven't switched over to it yet. Even in the absence of comments, I often like using it simply for readability: /\s*\((.*?)\)/ is just a lot harder to follow than /\s* \( (.*?) \)/x, and the latter could get away without a comment, IMH(-A)O.

Replies are listed 'Best First'.
Re^3: Best practice or cargo cult?
by Jenda (Abbot) on Aug 04, 2007 at 15:56 UTC

    Re /x ... is it? Let me see, the second example, what would that match? Let's see: some whitespace if any, space, opening brace, space, as little as possible of anything (and this is what I'm interested in), one space, closing brace and ... what the heck? OK, OK, back onto the trees, everything's wrong so let's go back and ignore the spaces, so we want some whitespace, opening brace, anything up till a closing brace and that's it. Why the heck cannot they write what they want right away?!?

    Unless the regexps is really complex /x just get's in the way, confusing what is and what is not part of the regexp and what's to be ignored. And if the regexp IS that complicated it's usually better to split it into several named pieces

    my $tagname = '[\w!][\w\d-]*'; my $paramname = '[\w!][\w\d-]*'; ... die "Malformed filter definition line: $line\n" unless $line =~ /^$tagname(?:\s+$tagname)*(?:\s*:\s*$paramname(?:\ +s+$paramname)*)?$/o; ...

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://556839]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2018-05-22 23:59 GMT
Find Nodes?
    Voting Booth?