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.
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.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; }
However, since you have three places you call this coderef, it may be better to just pass in the parameter:
Hope that helps,sub beta { my $self = shift; my $coderef = sub { $_[0] eq $gamma }; $self->_internal_sub($coderef); } # and ... push @foo, [ @bar ] if $coderef->($baz);
In reply to Re: Seek Help Refactoring
by Tanktalus
in thread Seek Help Refactoring
by jkeenan1
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |