SPUG: new member (and learner) w/ ONE quick syntax question
Can Subaykan
cansubaykan at hotmail.com
Mon Jun 24 15:09:17 CDT 2002
It seems like you have to jump through some trickery hoops with a 'do' or
'eval' or forcing an evaluation with a symbolic deref, am I overcomplicating
it, is there an easier way?:
$apples = 15;
$oranges = 15;
$fruits = 0;
$apples == $oranges ? eval {print "they are the same\n"; $fruits = $apples
+ $oranges} : print "Don't compare apples and oranges\n";
print "I have $fruits fruits in my fruit basket.\n\n";
also works with:
$apples == $oranges ? do {print "they are the same\n"; $fruits = $apples +
$oranges} : print "Don't compare apples and oranges\n";
and:
$apples == $oranges ? ${print "they are the same\n"; $fruits = $apples +
$oranges} : print "Don't compare apples and oranges\n";
but not with:
$apples == $oranges ? (print "they are the same\n"; $fruits = $apples +
$oranges) : print "Don't compare apples and oranges\n";
John.
----Original Message Follows----
From: dancerboy <dancerboy at strangelight.com>
To: "Baker, Stephen M" <stephen.m.baker at intel.com>, "perl mongers
(E-mail)" <spug-list at pm.org>
Subject: Re: SPUG: new member (and learner) w/ ONE quick syntax question
Date: Mon, 24 Jun 2002 12:53:22 -0700
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
_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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