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,


In reply to Re: Seek Help Refactoring by Tanktalus
in thread Seek Help Refactoring by jkeenan1

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.