NAME
Syntax::Infix::ConditionalSplice - a short-circuiting ?| operator for conditional list elements
VERSION
Version 0.01
SYNOPSIS
use Syntax::Infix::ConditionalSplice;
my @cmd = (
'prog',
$verbose ?| '--verbose', # included only when $verbose
$jobs > 1 ?| ('--jobs', $jobs), # a whole sub-list, conditionally
@files,
);
# equivalent, without the operator:
my @cmd = (
'prog',
($verbose ? ('--verbose') : ()),
($jobs > 1 ? ('--jobs', $jobs) : ()),
@files,
);
DESCRIPTION
Installs a lexically scoped infix operator ?| for conditionally splicing elements into a list. COND ?| LIST evaluates to LIST when COND is true and to the empty list () otherwise -- it is exactly COND ? LIST : (), but reads as one quiet element in the middle of a list instead of a parenthesised ternary with an easily-forgotten : () tail.
Two properties make it more than sugar a function could provide, and are the reason it is built on Infix::Custom's C-level build_op escape hatch:
It short-circuits.
LISTis only evaluated whenCONDis true, so$want ?| expensive()never callsexpensive()unless$want. A function call cannot do this -- its arguments are all evaluated first.It is context-aware. In list context the true branch flattens its list into the surrounding one; in scalar context
COND ?| LISTyields the list's last value (orundefwhen false), just like the ternary it mirrors.
The entire implementation is one line of C -- a COND_EXPR whose false branch is the empty-list stub:
return newCONDOP(0, lhs, rhs, newOP(OP_STUB, 0));
Precedence
?| binds at assignment precedence: tighter than comma (so $c ?| 'x' is a single list element) but looser than the comparison and logical operators (so the condition can be written without parentheses, e.g. $n > 3 ?| '--big' or $a && $b ?| $x). To make several elements conditional, parenthesise the right-hand side: $c ?| ('a', 'b').
Perl version
Requires perl 5.38 or newer (the PL_infix_plugin hook Infix::Custom builds on); it does not install on older perls.
SEE ALSO
Infix::Custom, Syntax::Infix::OptionalChain
AUTHOR
LNATION <email@lnation.org>
LICENSE AND COPYRIGHT
This software is Copyright (c) 2026 by LNATION <email@lnation.org>.
This is free software, licensed under The Artistic License 2.0 (GPL Compatible).