Enterprise Operator
Conditional list elements. You want an item in a list only if some condition is true:The Enterprise operator. It boldly goes where no ternary has gone before.()x!!
Named because it looks like the starship:
Engage.()x!! ( ) saucer section x engineering hull !! nacelles
Part 1: THE PROBLEM
You're building a list. Some elements are conditional:That ternary is ugly. And it gets uglier with more conditions.my @args = ( '--verbose', ($debug ? '--debug' : ()), '--output', $file, );
Part 2: THE SOLUTION
If $debug is true, you get '--debug'. If $debug is false, you get nothing.my @args = ( '--verbose', ('--debug') x !!$debug, '--output', $file, );
Clean. Readable. Logical.
Part 3: HOW IT WORKS
Break it down:When $debug is true:('--debug') x !!$debug PART MEANING ------------ ------------------------------------------ ('--debug') A list containing one string x List repetition operator !!$debug Boolean: 1 if true, '' (0) if false
When $debug is false:('--debug') x 1 # Repeat once: ('--debug')
Zero repetitions = empty list = element disappears.('--debug') x 0 # Repeat zero times: ()
.--. |o_o | |:_/ | // \ \ (| | ) /'\_ _/`\ \___)=(___/
Part 4: THE BANG BANG
Why !!$debug instead of just $debug?The x operator needs a number on the right. Boolean values in Perl:
But '' in numeric context is 0. So it works without !!... usually.True: 1 False: '' (empty string)
The !! makes it explicit. Self-documenting. And avoids edge cases where the condition might return something weird.
Part 5: MULTIPLE CONDITIONS
Stack them up:Each flag appears only if its condition is true. No nested ternaries. No ugly if blocks building up @cmd.my @cmd = ( 'rsync', ('-v') x !!$verbose, ('-n') x !!$dry_run, ('-z') x !!$compress, ('--delete') x !!$delete, $source, $dest, );
Part 6: MULTIPLE VALUES
Works with multiple elements too:If $user is set, you get ('--user', $user). If not, you get nothing.my @opts = ( ('--user', $user) x !!$user, ('--pass', $pass) x !!$pass, );
Both elements appear or neither does.
Part 7: HASH INITIALIZATION
Works in hash construction:Conditional hash entries without ternary mess.my %config = ( name => $name, (debug => 1) x !!$DEBUG, (verbose => 1) x !!$VERBOSE, );
Part 8: FUNCTION CALLS
Pass optional arguments:The optional parameter only appears when defined.some_function( $required_arg, ('optional' => $value) x !!defined($value), );
Part 9: VS TERNARY
Compare:Enterprise is:# Ternary approach my @list = ( 'always', ($cond ? 'maybe' : ()), ($other ? ('a', 'b') : ()), ); # Enterprise approach my @list = ( 'always', ('maybe') x !!$cond, ('a', 'b') x !!$other, );
- Shorter - More consistent - Easier to read in long lists
Part 10: GOTCHAS
Parentheses matter:Without parens, x does string repetition, not list repetition.'item' x !!$cond # Wrong: 'item' x 1 = 'item' (scalar) ('item') x !!$cond # Right: ('item') x 1 = ('item') (list)
Also watch for precedence:
('a', 'b') x !!$cond && $other # Wrong: !! binds to $cond only ('a', 'b') x !!($cond && $other) # Right: condition grouped
Part 11: REAL WORLD EXAMPLE
Building a database query:Clean SQL construction with optional clauses.my @clauses = ( "SELECT * FROM users", ("WHERE active = 1") x !!$active_only, ("AND role = ?") x !!$role, ("ORDER BY $sort") x !!$sort, ("LIMIT $limit") x !!$limit, ); my $sql = join ' ', @clauses;
Part 12: COMMAND LINE BUILDER
All the complexity hidden in clean, scannable code.my @ffmpeg = ( 'ffmpeg', ('-y') x !!$overwrite, '-i', $input, ('-c:v', $vcodec) x !!$vcodec, ('-c:a', $acodec) x !!$acodec, ('-b:v', $bitrate) x !!$bitrate, ('-ss', $start) x !!$start, ('-t', $duration) x !!$duration, $output, ); system @ffmpeg;
Part 13: THE NAME
Enterprise. As in USS Enterprise. The starship.Okay, you need imagination. But Perl operators have whimsical names, and this one stuck because it's memorable.()x!! ( ) Saucer section (primary hull) x Engineering section (secondary hull) !! Warp nacelles ()x!! ___ / \ ( ) \___/ | x /|\ / | \ !! | !!
Also called the "conditional list element operator" if you want to be boring about it.
()x!! _______ / \ ( () ) \_______/ | x /|\ / | \ !! | !! | ENGAGE Conditional elements at warp speed
Created By: Wildcard Wizard. Copyright 2026