in reply to Re^3: Why is "any" slow in this case?
in thread Why is "any" slow in this case?

Yeah I see your point now.

Seems like Perl is playing safe if a lexpad is required for the closure, even if the closed over lexical variable doesn't change, like here:

$ perl use v5.14; my @subs; sub dosomething :prototype(&) { push @subs, $_[0]; } my $x; for (1..2) { dosomething { $x+1 } } for (1..2) { my $x; dosomething { $_+1 } } say for @subs; __END__ CODE(0x64405f0de700) CODE(0x64405f10b340) CODE(0x64405f118bb0) CODE(0x64405f118bb0)

BTW: Clever move to push the code-refs to @subs and keeping them alive instead of printing them out.

Like this you are avoiding that Perl tries to reuse a freed ref again! IOW if we see the same ref again we are sure teh sub wasn't generated again.

Cheers Rolf
(addicted to the Perl Programming Language :)
see Wikisyntax for the Monastery