Well, they all do different things.
However, all other factors are equal,
push @list, 'foo' if $foo;
- This is a statement that directly conveys its meaning, so it should be preferred.
If you need to pause and think about a statement that supposedly does something as simple as clicking an array element, you are doing something wrong.
my @list = ( $foo ? 'foo' : (), $bar ? 'bar' : (), );
it might be OK if it is part of some colossal initialization that runs elsewhere.
I think using
my @list = ( ('foo') x!! $foo, ('bar') x!! $bar, );
indicates what the programmer has - how can I put this? - problems .
By the way, there is no such thing called the composite operator x!! . !! double logical negation . It converts an arbitrary false value to a specific but false value (empty string), which gives 0 when used where perl expects a number. The true value is converted to 1 . Here !! works on $foo and $bar , and its entry is x!! $foo x!! $foo unnecessarily confuses the meaning of the code.
x is a repetition operator. Thus,
('foo') x !!$foo;
means repeat ('foo') one or more times, depending on whether $foo true or false, respectively.
PS: Of course, it turned out that there is an article in the PerlMonks article that introduces the so-called Boolean list squash operator. I read the article and found it inconclusive.