Perl Style: Never define "TRUE" and "FALSE"

  • The language understands booleans. Never define them yourself! This is terrible code:

         $TRUE  = (1 == 1);
         $FALSE = (0 == 1);
    
         if ( ($var =~ /pattern/ == $TRUE  ) { .... }
         if ( ($var =~ /pattern/ == $FALSE ) { .... }
         if ( ($var =~ /pattern/ eq $TRUE  ) { .... }
         if ( ($var =~ /pattern/ eq $FALSE ) { .... }
    
         sub getone { return "This string is true" }
    
         if ( getone() == $TRUE  ) { .... }
         if ( getone() == $FALSE ) { .... }
         if ( getone() eq $TRUE  ) { .... }
         if ( getone() eq $FALSE ) { .... }
    
  • Imagine the silliness of this progression, and stop at the first one.

         if (    getone() )                       { .... }                   
         if (    getone() == $TRUE  )             { .... }
         if (   (getone() == $TRUE) == $TRUE  )           { .... }
         if ( ( (getone() == $TRUE) == $TRUE) == $TRUE  ) { .... }
    

Forward to Embrace Pattern Matching
Back to Don’t Overdo `?:’
Up to index

Copyright © 1998, Tom Christiansen All rights reserved.

Tags

Feedback

Something wrong with this article? Help us out by opening an issue or pull request on GitHub