Assigning using ternary operator?

I'd usually write this as:

$review = ( defined($model->test) ? 1 : '' );

where the parentheses are for clarity for other people reading the code.


You have a precedence problem. What you have is the same as

( defined($model->test) ? $review="1" : $review ) = '';

You could make it work with parens.

my $review; defined($model->test) ? ( $review='1' ) : ( $review='' );

But it's much cleaner to move the assignment out.

my $review = defined($model->test) ? '1' : '';

Moving along, there's no point in checking if the value is defined at all. Seeing as the only two possible values are undef and the string 1, a simple truth test would do.

my $review = $model->test ? '1' : '';

In fact, since you want true values unchanged, you could simply use the following

my $review = $model->test || '';

Finally, do you really need to change undef into to an empty string? If not, you can simply use the following:

my $review = $model->test;