SPUG: new member (and learner) w/ ONE quick syntax question

dancerboy dancerboy at strangelight.com
Mon Jun 24 14:53:22 CDT 2002


At 12:07 pm -0700 2002-06-24, Baker, Stephen M wrote:
>	when using the ternary operator ?, is the syntax:
>
>	expression ? if_true : if_false;
>
>	possible to modify so that if_true or if_false are compound
>expressions??

Depends on what exactly you mean by "compound expressions"... but the 
simple answer is Yes:

     expression_1
     ? ( expression_2, expression_3, expression_4)
     : ( expression_5, expression_6, expression_7)

is a perfectly legal Perl expression.  But it's probably a bad idea. 
If your if_true/ if_false expressions are going to be that 
complicated, you should *either* replace the ternary operator with an 
if/else statement, like this:

    if ( expression ) {
       if_true;
    } else {
       if_false;
    }

*or* you should split your if_true and if_false expressions into 
separate subroutines, like this:

     sub if_true {
         ... do something ...
     }

     sub if_false {
         ... do something else ...
     }

     [...]

     expression ? if_true() : if_false();

This will make your code much more legible.

-jason

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
     Seattle Perl Users Group (SPUG) Home Page: http://seattleperl.org




More information about the spug-list mailing list