my $var;
if($cond) {
$var = 'this';
} else {
$var = 'that';
}
to be written as
my $var = $cond ? 'this' : 'that';
which is one line compared to 5, 6 or 7, depending on indentation style.
We have more of such subtle operators, eg. ||= vs. //=, where the first checks the LHS truthiness, and the second checks the LHS definedness.
Writing
$var = $var ? 'this' : 'that';
just looks and feels as silly as
$value = $value * 5;
$next = $next + 1;
instead of
$value *= 5;
$next++;
For the same reasons, I like the compound operator x!! so much, because it lets me set up a parameter list based on truthiness of variables:
$result = build_shed(
logs => 24,
(screws => 120) x!! @screwdrivers,
(nails => 360) x!! @hammers,
);
Otherwise, I'd had to say:
my %materials = (logs => 24);
$materials{screws} = 120 if @screwdrivers;
$materiasl{nails} = 360 if @hammers;
$result = build_shed(%materials);
While the second variant is one line less, I regard the first variant as much more readable, and I don't have to introduce a temporary hash just for the sake of building function arguments. Note that in the second variant, the hash name is misspelt as materiasl at the 'nails' case, small bug caught by strict, but annoying. So, instead of
$var = $var ? 'this' : 'that';
I'd rather like to see
$var ?= 'this' : 'that';
because it binds the condition to the LHS in the same way as ||= and //= do.
I can hardly imagine a reason to map non-Boolean values to a previously Boolean $var without confusing any maintainer.
Who said that $var was boolean? It could hold any value, and this value is checked for truthiness.
Anyways - de gustibus non est disputandem.
perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
|