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

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

by GrandFather (Saint)
on Aug 26, 2019 at 21:10 UTC ( [id://11105089]=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re^5: regex return true instead of false (precedence)
by Marshall (Canon) on Aug 26, 2019 at 21:29 UTC
    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.

      I agree with your and GrandFather's leftist leaning when it comes to formatting complex logical expressions, but I think it's tangential to the main point made by haukex here: the statements
          if     (    condition  ) {} else {code }
          unless (    condition  )         {code }
          if     ( ! (condition) )         {code }
      are all exactly equivalent, so the condition expression within them does not need to change at all.


      Give a man a fish:  <%-{-{-{-<

        Well part of the point that I'm trying to make is that condition should change to make the logical sense of this easier to understand.

        unless ( (!($args =~ /\-rs(\s+)(\S+)/)) && (!($args =~ /\-p(\s+)(\S+)/)) && (!($args =~ /\-P(\s+)(\S+)/)) ) {..... # I figure this is easier to understand: if ( $args =~ /\-rs(\s+)(\S+)/ #update changed unless->if or $args =~ /\-p(\s+)(\S+)/ or $args =~ /\-P(\s+)(\S+)/ ) {....
        I was trained as a hardware engineer. DeMorgan's Theorem is an important part of hardware design. An AND or an OR gate is actually pretty rare. The most common gate is NAND, followed by NOR. The reason for that is that the fastest gates have a logical inversion - that is due to the way the hardware works. Inverting that output slows things down.

        Changing the formulation of condition can make things easier to understand. The compiler should generate similar code for the above examples. I just think that the second is easier for a human to understand.

Re^5: regex return true instead of false (precedence)
by rsFalse (Chaplain) on Aug 27, 2019 at 16:40 UTC
    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.
      if( $args !~ / (*FAIL) | \-rs(\s+)(\S+) # | \-? (\s+)(\S+) | \-P (\s+)(\S+) | \-p (\s+)(\S+) /x ){ ... }

      I don't understand the purpose of the  (*FAIL) operator (see Special Backtracking Control Verbs in perlre in Perl versions 5.10+ (update: see also Backtracking control verbs in perlretut - but that's not actually as extensive as the discussion in perlre)) in the quoted regex. At any position in the  | alternation,  (*FAIL) will simply force the RE to try the next alternation; if it's at the last position, all the preceding pattern matches would have failed and the alternation would fail anyway without (*FAIL).

      c:\@Work\Perl\monks>perl -wMstrict -le "use 5.010; ;; for my $s (qw(XXX ppp)) { print qq{for '$s' string}; if ($s !~ m{ (*F) | p | q }xms) { print ' with (*F): no match, !~ +true' } if ($s !~ m{ Z | p | q }xms) { print ' with Z: no match, !~ +true' } if ($s !~ m{ p | q }xms) { print ' no (*F): no match, !~ +true' } print ' -------'; } " for 'XXX' string with (*F): no match, !~ true with Z: no match, !~ true no (*F): no match, !~ true ------- for 'ppp' string -------
      Note that for all these variations, for string 'XXX' there is never a match (!~ is always true); string 'ppp' always matches (!~ always false).

      ... not equivalently, because there are 8 capturing groups instead of 2 ...

      If you're using  (*FAIL) you must be using Perl version 5.10+, so you also have the  (?|pattern) "branch reset" operator (see Extended Patterns in perlre). This allows you to go back to having two capture groups again!

      Update: Changed example code slightly to better reflect discussion.


      Give a man a fish:  <%-{-{-{-<

        "I don't understand the purpose of the (*FAIL) operator" -- AnomalousMonk.

        It has the same purpose as zero in separated regex example. It allows '||' or '|' be written before first conditional. And then all conditionals have the same line-format. For me it is more readable :)

        It is similar to:
        if( 0 ){ ; } elsif( condition_1 ){ ... } elsif( condition_2 ){ ... } elsif( condition_3 ){ ... }
        ...which is sometimes a bit more readable. Also then I can easily change the sequence of conditions by only swapping lines (including the line with the first condition).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-25 20:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found