Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re^2: regex return true instead of false (precedence)

by Ratazong (Monsignor)
on Aug 26, 2019 at 13:52 UTC ( #11105064=note: print w/replies, xml ) Need Help??

in reply to Re: regex return true instead of false (precedence)
in thread regex return true instead of false

The easiest way to get around precedence-issues is to define precedence yourself by using (a lot of) brackets. So

unless (! $args =~ /\-rs(\s+)(\S+)/ && ! $args =~ /\-p(\s+)(\S+)/ && ! + $args =~ /\-P(\s+)(\S+)/) {
will become
unless ((!($args =~ /\-rs(\s+)(\S+)/)) && (!($args =~ /\-p(\s+)(\S+)/) +) && (!($args =~ /\-P(\s+)(\S+)/))) {
I agree it looks a bit confusing, but modern editors help you by showing the matching bracket. And you don't have to think about rules (which might be counter-intuitive (like ! vs =~) or different in other programming languages).

HTH, Rata

Replies are listed 'Best First'.
Re^3: regex return true instead of false (precedence)
by LanX (Sage) on Aug 26, 2019 at 16:59 UTC
    I agree that it's better to over use brackets than forgetting them.

    On the other hand there's a good reason why Perl has that many operators.

    And, not, or won't bite you in Boolean expressions because they have the lowest precedence. (But I use brackets when combining them, I need a complicated mnemonic to remember that and has higher precedence than or)

    And !~ is really underused.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Re^3: regex return true instead of false (precedence)
by Marshall (Canon) on Aug 26, 2019 at 18:00 UTC
    I prefer to use indenting to make all the parens more clear. Also, this "unless not" syntax can be confusing.
    I think I got this right, but it is easy to make a mistake. Consider:
    # X is the "default action" # do X with an exception that when A,B and C are all false # then don't do X unless (!A and !B and !C) {X} # if anybody is true then they all can't be false # so we do X unless anybody is true (meaning they # can't all be false) unless (A or B or C) {X} ------- unless ( (!($args =~ /\-rs(\s+)(\S+)/)) && (!($args =~ /\-p(\s+)(\S+)/)) && (!($args =~ /\-P(\s+)(\S+)/)) ) {..... # I figure this is easier to understand: unless ( $args =~ /\-rs(\s+)(\S+)/ or $args =~ /\-p(\s+)(\S+)/ or $args =~ /\-P(\s+)(\S+)/ ) {....

      If you break a condition like that over multiple lines (and for a complicated or long condition I recommend it) put the operators at the front of the lines so they don't get "lost" at the back:

      if ( $args =~ /\-rs(\s+)(\S+)/ || $args =~ /\-p(\s+)(\S+)/ || $args =~ /\-P(\s+)(\S+)/ )
      Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
        Actually, I like that better.
        As I remember, a senior Monk yelled at me once when I posted something like that. But yes this is the way that I would do it in my code. I didn't want to get into an argument that detracts from the main point.
        Update: As I remember the argument against this was like "injecting spacing like that is bad because it is manually done and IDE's don't do it that way". BS. The compiler doesn't give a darn about spaces. Whitespace is added to help the humans. If it helps the humans, it is a good idea.
        Also you can waste one line of code for making first conditional to have the same line-format as others:
        if( not 0 || $args =~ /\-rs(\s+)(\S+)/x # || $args =~ /\-? (\s+)(\S+)/x || $args =~ /\-p (\s+)(\S+)/x || $args =~ /\-P (\s+)(\S+)/x ){ ... }
        ...or equivalently inside regex:
        if( $args !~ / (*FAIL) | \-rs(\s+)(\S+) # | \-? (\s+)(\S+) | \-P (\s+)(\S+) | \-p (\s+)(\S+) /x ){ ... }
        edit: not equivalently, because there are 8 capturing groups instead of 2 :)

        Of course regex can be simplyfied as AnomalousMonk have shown here Re: regex return true instead of false.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (1)
As of 2023-03-31 00:43 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (74 votes). Check out past polls.