Perl-pass code block as a parameter in parentheses

Is it possible to pass a block of code to a sub using parenthesis syntax?

those. when I write

List::MoreUtils::any { defined ($_) } (undef, undef, 1);

it works. But when I try to add parentheses

List::MoreUtils::any ( { defined ($_) } , (undef, undef, 1) );

this is interpreted as an anonymous error message hash. Neither acceleration nor the use of eval helps.

The idea of ​​all the fuss is that the challenge is part of the expression, i.e.

if (first_index { defined (${$_})} $jms_positions > $jms_positionals_seen )

some operator following the arguments may be executed before the call, creating an undesirable result.

+3
source share
3 answers

Anonymous routine declared with syntax

sub { say "The sub with no name!" };

Perl , , sub , Perl. . parens , -ref.

,

List::MoreUtils::any( sub { defined }, undef, undef, 1 );

.

+15

, .

$ perl -ce'map({ "..." } @a)'
-e syntax OK

$ perl -ce'grep({ "..." } @a)'
-e syntax OK

$ perl -ce'sub f(&@); f { "..." } @a'
-e syntax OK

$ perl -ce'sub f(&@); f({ "..." } @a)'
Array found where operator expected at -e line 1, near "} "
        (Missing operator before  ?)
syntax error at -e line 1, near "} @a"
-e had compilation errors.

.

( any { defined } (undef, undef, 1) )
+5

Just paste sub:

List::MoreUtils::any ( sub { defined ($_) } , (undef, undef, 1) );
+3
source

All Articles