my $x = if ($cond) { 1 } else { 2 }; # Syntax error
Because of this, Perl introduces a second syntax to plug the gap: my $x = $cond ? 1 : 2;
That expression is neither obtuse nor hard to read. my $fee =
$is_member
? ($is_student ? $student_member_fee : $member_fee)
: ($is_student ? $student_fee : $standard_fee);
It's equivalent to the following code that uses `if`: my $fee;
if ($is_member) {
if ($is_student) {
$fee = $student_member_fee;
}
else {
$fee = $member_fee;
}
}
else {
if ($is_student) {
$fee = $student_fee;
}
else {
$fee = $standard_fee;
}
}
But ideally you would want to write this: my $fee =
if ($is_member) {
if ($is_student) {
$student_member_fee
}
else {
$member_fee
}
}
else {
if ($is_student) {
$student_fee
}
else {
$standard_fee
}
};
Though, of course, perl5 doesn't allow that.
> the goodness of language for long code is inversely proportional to how nice it is for shell one-liners
Well, except for this bit. Oil or fish and awk are perfectly fine for one liners but don't repeat the mistakes of posix shell syntax.