note
Aristotle
<p>OK, so my example only used single-element lists, and most conditions were similar, so it was an insufficiently ornery problem to show that only the ternary and [doc://push] are generically appropriate. Here’s an example to disqualify [doc://grep]:</p>
<c>
my $name = join '-', (
@prefix,
( $do_use_path ? split /:/, $path : () ),
$name,
$suffix,
);
</c>
<del>
<p>The only way I can think of to write that with [doc://grep] is thus:</p>
<c>
my $name = join '-', (
@prefix,
( map @$_, grep { $do_use_path } [ split /:/, $path ] ),
$name,
$suffix,
);
</c>
</del>
<ins><p>D’oh, I see it now. Hmm…</p></ins>
<p>That looks a lot worse than the ternary to me. [doc://grep] is fine for problems with enough regularity, but when regularity is absent (here trivially achieved by having only a single conditional sublist, so there’s nothing to abstract), ternary/[doc://push]/<tt>x!!</tt> is necessary – and of these, only <tt>x!!</tt> requires no unnecessary verbiage:</p>
<c>
my $name = join '-', (
@prefix,
( split /:/, $path ) x!! $do_use_path,
$name,
$suffix,
);
</c>
<p>I wish modifiers could be applied to expressions, then this would be crystal clear to write:</p>
<c>
my $name = join '-', (
@prefix,
( split /:/, $path ) if $do_use_path,
$name,
$suffix,
);
</c>
<p align="right" class="pmsig pmsig-114691"><i>Makeshifts last the longest.</i></p>
564792
564872