<?xml version="1.0" encoding="windows-1252"?>
<node id="565251" title="Re^2: Secret Perl Operators: the boolean list squash operator, x!! (grep disqualified)" created="2006-08-02 11:51:06" updated="2006-08-02 07:51:06">
<type id="11">
note</type>
<author id="114691">
Aristotle</author>
<data>
<field name="doctext">
&lt;p&gt;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]:&lt;/p&gt;

&lt;c&gt;
my $name = join '-', (
	@prefix,
	( $do_use_path ? split /:/, $path : () ),
	$name,
	$suffix,
);
&lt;/c&gt;

&lt;del&gt;

&lt;p&gt;The only way I can think of to write that with [doc://grep] is thus:&lt;/p&gt;

&lt;c&gt;
my $name = join '-', (
	@prefix,
	( map @$_, grep { $do_use_path } [ split /:/, $path ] ),
	$name,
	$suffix,
);
&lt;/c&gt;

&lt;/del&gt;

&lt;ins&gt;&lt;p&gt;D’oh, I see it now. Hmm…&lt;/p&gt;&lt;/ins&gt;

&lt;p&gt;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]/&lt;tt&gt;x!!&lt;/tt&gt; is necessary – and of these, only &lt;tt&gt;x!!&lt;/tt&gt; requires no unnecessary verbiage:&lt;/p&gt;

&lt;c&gt;
my $name = join '-', (
	@prefix,
	( split /:/, $path ) x!! $do_use_path,
	$name,
	$suffix,
);
&lt;/c&gt;

&lt;p&gt;I wish modifiers could be applied to expressions, then this would be crystal clear to write:&lt;/p&gt;

&lt;c&gt;
my $name = join '-', (
	@prefix,
	( split /:/, $path ) if $do_use_path,
	$name,
	$suffix,
);
&lt;/c&gt;

&lt;p align="right" class="pmsig pmsig-114691"&gt;&lt;i&gt;Makeshifts last the longest.&lt;/i&gt;&lt;/p&gt;</field>
<field name="root_node">
564792</field>
<field name="parent_node">
564872</field>
</data>
</node>
