[ABE.pm] ternary operator

Ted Fiedler tfiedler at elevenfour.com
Fri Jul 22 18:08:32 PDT 2005


try this

my $tempDate = ( defined ($endYYYYMM) ) ? $endYYYYMM."28"
                                         : $YYYY.$MM."28";

Faber Fedor wrote:
> I'm confoozled about the ternary operator.  I want to do this:
> 
> if($endYYYYMM) {
>     $tempDate = $endYYYYMM."28";
> }else{
>     $tempDate = $YYYY.$MM."28";
> }
> 
> IOW, if $endYYYYMM *exists* then $tempDate = $endYYYYMM."28" otherwise,
> if $endYYYYMM *doesn't exist* (the user didn't pass it in on the command
> line) set $tempdate to something different.
> 
> That works fine.
> 
> I thought I could be more succint by saying
> 
> $endYYYYMM ? $tempDate = $endYYYYMM."28" : $tempDate = $YYYY.$MM."28";
> 
> but that always executes the false statment, e.g. 
> $tempDate = $YYYY.$MM."28".
> 
> I've run into this problem in a few other places, so I know it's not
> related to the variables but rather my understanding of things.
> 
> What am I missing?
> 
> 



More information about the ABE-pm mailing list