perl5 - Perl - pass code block as parameter inside parentheses -
is possible pass block of code sub using "parentheses" syntax?
i.e. when write
list::moreutils::any { defined ($_) } (undef, undef, 1);
it works. when try add parentheses
list::moreutils::any ( { defined ($_) } , (undef, undef, 1) );
this interpreted anonymous hash, giving error message. neither escaping nor using eval helps.
the idea behind fuss if call part of expression, i.e.
if (first_index { defined (${$_})} $jms_positions > $jms_positionals_seen )
some operator following arguments might executed before call, producing undesired result.
an anonymous subroutine declared syntax
sub { "the sub no name!" };
perl's prototype system allows special exception code block first parameter, in case can leave off leading sub
, pass block, similar perl builtin. works when calling in parentheses-less style. using parens causes parser think want pass hash-ref.
so can
list::moreutils::any( sub { defined }, undef, undef, 1 );
if insist on using parens.
Comments
Post a Comment