in reply to How to avoid $_ in grep

Would this work for you? Just take a reference to $_.

my @array_2 = grep {my $element = \$_; $$element =~ /foo/} @array_1;

lupey

Replies are listed 'Best First'.
Re^2: How to avoid $_ in grep
by ikegami (Patriarch) on Nov 13, 2007 at 21:10 UTC

    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;