Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Best practice or cargo cult?

by Argel (Prior)
on Jun 20, 2006 at 22:00 UTC ( #556534=note: print w/ replies, xml ) Need Help??


in reply to Best practice or cargo cult?

Here's a rough summary of TheDamian's reasoning:

/x (PBP pgs 236-237)

RegEx's are really programs and thus should follow the other guidelines presented in the book (the most obvious being to comment them).

/m (PBP pgs 237-239)

Perl should behave the way most people think it will behave. And thanks to common line-based UNIX utilities like sed, grep, and awk the default Perl behavior for ^ and $ will not behave as expected (by most people). So use /m to align expectations.

/s (PBP pgs 240-241)

Pretty much the same argument as used in /m -- Perl should behave the way people think it will behave.


Comment on Re: Best practice or cargo cult?
Re^2: Best practice or cargo cult?
by sfink (Deacon) on Jun 22, 2006 at 05:47 UTC
    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.

      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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (9)
As of 2014-07-23 09:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (138 votes), past polls