in reply to
Better Short Answer:
$a1=2; $b1=10; ($a1 > $b1) ? ($int = ($a1/$b1)) : ($int = ($b1/$a1));
What it should really be:
$a1=2; $b1=10; $int = ($a1 > $b1) ? ($a1/$b1) : ($b1/$a1); print $int;
defined($int = ($a1/$b1))
use strict ;
use warnings ;
# Use Better Variable Names.
my $a1 = 2 ;
my $b1 = 10 ;
my $int = ( $a1 > $b1 ) ? ( $a1 / $b1 ) : ( $b1 / $a1 ) ;
will always evaluate to true regardless of the values of $a1 and $b1. If you want to check if both $a1 and $b1 are defined you might want to nest it in there like:
my $int = ( defined( $a1 ) and defined( $b1 ) ) ? ( ( $a1 > $b1 ) ? (
+$a1 / $b1 ) : ( $b1 / $a1 ) ) : die( '$a1 and $b1 need to be defined'
+ ) ;
The other thing to note is that the the Ternary Operator, being an Operator, will return something and so you dont need to do the $int =
in both the if and else parts.