Perl Style: Switch by Using do{} Creatively

  • Another interesting approach to a switch statement is arrange for a do block to return the proper value:

       $amode = do {
           if     ($flag & O_RDONLY) { "r" }       # XXX: isn't this 0?
           elsif  ($flag & O_WRONLY) { ($flag & O_APPEND) ? "a" : "w" }
           elsif  ($flag & O_RDWR)   {
               if ($flag & O_CREAT)  { "w+" }
               else                  { ($flag & O_APPEND) ? "a+" : "r+" }
           }
       };
    

Forward to Switch with for via && and ||
Back to Learn to Switch with for
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

TPRF Gold Sponsor
TPRF Silver Sponsor
TPRF Bronze Sponsor