in reply to Seek Help Refactoring

In addition to some perfectly reasonable ideas already presented, here's the way I do a similar thing. In my case, I'm going to use $baz rather than $self->{baz} to show that it's a lexical. Or, more to the point, that $baz may not be known until you're three levels deep inside _internal_sub and completely dynamic.

sub alpha; # unchanged from your example. sub beta { my $self = shift; my $coderef = sub { $_ eq $gamma }; $self->_internal_sub($coderef); } sub _internal_sub { my $self = shift; my $coderef = shift; foreach ... if ... if ... if ... local $_ = $baz; push @foo, [ @bar ] if $coderef->(); } } } else { local $_ = $baz; push @foo, [ @bar ] if $coderef->(); } } local $_ = $baz; push @foo, [ @bar ] if $coderef->(); \@foo; }
I prefer the $coderef->() version over &$coderef because the former says "dereference this code reference, and call it with no parameters" while the latter says "call this piece of code, (ignoring prototypes - not relevant with code refs), passing in @_." The former is what I'm doing, not the latter.

However, since you have three places you call this coderef, it may be better to just pass in the parameter:

sub beta { my $self = shift; my $coderef = sub { $_[0] eq $gamma }; $self->_internal_sub($coderef); } # and ... push @foo, [ @bar ] if $coderef->($baz);
Hope that helps,