return $is_true ? 'is true!' : 'is false'; # is just a one line way of saying if ($is_true) { return 'is true!'; } else { return 'is false!'; }
Let me elaborate a bit more (aka update):
You can even nest them:
$a will contain one of the strings depending upon the value of $is_true and $was_true.$a = $is_true ? 'is true!' : $was_true ? 'was true' : 'was never true' +;
The 'hook' operator (as i like to call it) is best used for assignments, as my examples showed. Don't be tempted to do this, as it is just bad coding practice:
$some_boolean ? do_sub1() : do_sub2();
--------------------------------------------------
perl -le '$x="jeff";$x++ for(0..4482550);print $x'
In reply to (jeffa) Re: What does the ternary statement actually do?
by jeffa
in thread What does the ternary statement actually do?
by Mr.T
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |