Bob Showalter wrote:
> Ravi Malghan wrote:
> > Hi: I have this statement which checks for existence
> > of the $VAL variable and performs certain actions
> >
> > if($VAL){
> > $VAL = "$VAL:$expr"; }
> > else {
> > $VAL = "$expr"; }
> >
> > Can this 4 line statement be reduced to a single line using "?"
> > operator?
>
> $VAL = $VAL ? "$VAL:$expr" : $expr;
At last! Thanks Bob, this /is/ how to use the conditional operator,
while
$VAL ? ( $VAL = "$VAL:$expr" ) : ( $VAL = $expr )
/isn't/. ?: is an /operator/. It happens to have three operands
instead of the usual two or 1, but it is meant for deriving a
new value from three others, like a function.
sub conditional {
my ($test, $val1, $val2);
return $val1 if $test;
return $val2;
}
$VAL = conditional ($VAL, "$VAL:$expr", $expr)
Rob
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]