in reply to Re: How to avoid $_ in grep
in thread How to avoid $_ in grep
If you don't mind the usually small cost of copying,
my @array_2 = grep { my $element = $_; $element->func() eq 'TEST' } @array_1;
It's basically the same as my ($arg1, $arg2) = @_; in subs.
If you're dealing with very long strings and want to avoid the cost of copying without having to deal with references, you can create an alias,
my @array_2 = grep { our $element; local *element = \$_; $element->func() eq 'TEST' } @array_1;
or
use Data::Alias qw( alias ); my @array_2 = grep { alias my $element = $_; $element->func() eq 'TEST' } @array_1;
|
|---|