I'd like to suggest what I shall dub the "Readable" approach:
sub maybe ($$) { $_[1] ? $_[0] : () }
my @list = (
maybe("foo", $foo),
maybe("bar", $bar),
);
It has many of the benefits of this alleged x!!
operator (though slightly longer), but with the added bonus that you can actually understand your code when you come back to it later.
EDIT: The prototype doesn't help Perl parse any better and doesn't let us ditch the parenthesis, it just prevents Perl from doing things we don't want. If we want to ditch the parens, we have to do some work. Here's an alternate version that works without parenthesis:
sub maybe {
my($var, $cond, @rest) = @_;
return @rest unless $cond;
return $var, @rest;
}
my @list = (
maybe "foo", $foo,
maybe "bar", $bar,
);
No matter what we do with prototypes, Perl will try to parse it as maybe("foo", $foo, maybe("bar", $bar))
, so if we want to ditch the parenthesis, we just have to make that give the correct result.