Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^2: Question about regex.

by GrandFather (Saint)
on Sep 29, 2020 at 20:11 UTC ( #11122347=note: print w/replies, xml ) Need Help??


in reply to Re: Question about regex.
in thread Question about regex.

"local $/="";
"If you wanted to format your regex nicely and have whitespace ignored, you could use the /x modifier"

Anyone else feel a dissonance here? I'd format the assignment (yes, it is an assignment) as:

local $/ = "";

On first glance the assignment looks like $/= "" to me and that's not right. An eye blink later it turns into $ /= "" which doesn't work either. It's only after two glances and a hard look that it resolves into $/ = "";. Using white space to reduce cognitive load is a Good Thing

Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

Replies are listed 'Best First'.
Re^3: Question about regex.
by haukex (Bishop) on Sep 29, 2020 at 20:32 UTC
    Anyone else feel a dissonance here?

    Yes, you do have a point, I didn't need to be stingy with those two spaces. Though I did say "If you wanted to format your regex nicely", and not "you should format your regex nicely" (and noone else in the thread used /x either) ;-)

    Edit: missed a word on c&p

      I get a bit touchy about white space. Many of my work mates eschew spaces wherever possible and at the other extreme I see stuff like function( param1 , param2 , param3 ); which hurts my brain almost as much as no spaces. My rule of thumb is to use white space where possible as you would for prose on the basis that that is what we are most used to parsing so we get a fair bit of the parsing work essentially for free.

      Interestingly regexen are something of an exception for me. Unless they are fairly complicated I don't use /x. When I do use /x I often break the regex up over several lines and comment each part. Mostly though I keep the regexen fairly simple and do complicated stuff where it's easier to maintain.

      Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
        My rule of thumb is to use white space where possible as you would for prose on the basis that that is what we are most used to parsing so we get a fair bit of the parsing work essentially for free.

        I also tend to use whitespace around operators and after commas, but at the same time, personally I can read things like $/=""; easily, which is why I don't always put whitespace around operators if the expression is short - if I'm coding for myself, I just base it on my feeling of readability. If I had to make a choice for a best practice recommendation though, I'd say whitespace around operators is good.

        Interestingly regexen are something of an exception for me.

        For me, regexes don't always read easily, especially if there's a lot of nested parens going on, so when trying to understand a regex, I'll often add the /x and whitespace.

        Your post exactly matches my own approach.

        Interestingly regexen are something of an exception for me. Unless they are fairly complicated I don't use /x.

        I've been thinking about this because it is, as you say, the exception. My conclusion is that often enough a regex will contain a short section of prose from somewhere else and it is trivial to paste this in from the other source. With /x one would then have to go through it manually and escape all the spaces or replace them with something else. The problem of course compounds with interpolation:

        my $pre = 'Put the needle on the record'; my $post = 'the drum beats go like'; if ($corpus =~ /($pre\W*){3}when $post this!/) { $volume++; }

        Good luck managing that with /x - far better without. When the regex starts to become too large/unwieldy I just split it into several shorter ones which are then combined which I find helps the logical flow too.

        For completeness: the one operator I do sometimes cuddle with operands is the range operator but that's likely a relic from some shells (such as bash) where whitespace around the same construct can cause problems.


        🦛

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2021-06-23 06:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (115 votes). Check out past polls.

    Notices?