Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: regex return true instead of false (precedence)

by LanX (Saint)
on Aug 26, 2019 at 11:46 UTC ( [id://11105061]=note: print w/replies, xml ) Need Help??


in reply to regex return true instead of false

your ! has higher precedence than =~ , please note the (parens) due to -p flag in B::Deparse

d:\exp>perl -MO=Deparse,-p -e"if (! $args =~ /\-rs(\s+)(\S+)/ && ! $ar +gs =~ /\-p(\s+)(\S+)/ && ! $args =~ /\-P(\s+)(\S+)/){1}" if (((((!$args) =~ /\-rs(\s+)(\S+)/) and ((!$args) =~ /\-p(\s+)(\S+)/) +) and ((!$args) =~ /\-P(\s+)(\S+)/))) { '???'; } -e syntax OK

I'd say either use not instead of ! or just !~ instaed of =~

and I'd also recommend using and instead of &&

EDIT

you might be interested in reading

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

Replies are listed 'Best First'.
Re^2: regex return true instead of false (precedence)
by Ratazong (Monsignor) on Aug 26, 2019 at 13:52 UTC

    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

      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

      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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-19 05:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found