my $var = sub { $_[0]->{this} eq 'that' }; foreach my $hash_ref (@a) { if ($hash_ref->{this} eq 'that') { ... } if ($var->($hash_ref)) { ... } }