Suppose that I have two subroutines, each of which has nested if/else conditions, which are identical in all respects except that, deep inside the if/else nest, one sub performs an action in all circumstances, while the other performs an action only if some condition X is true. The action may be called at a number of points within the subroutine depending on the value of arguments.

I would like to be able to refactor out all the code shared by the two subs but am wondering what the best way to handle the differences. Some pseudo-code:

sub alpha { my $self = shift; ... foreach ... if ... if ... if ... push @foo, [ @bar ]; } } } else { push @foo, [ @bar ]; } } push @foo, [ @bar ]; return \@foo; } sub beta { my $self = shift; ... foreach ... if ... if ... if ... push @foo, [ @bar ] if $self->{baz} eq $gamma; } } } else { push @foo, [ @bar ] if $self->{baz} eq $gamma; } } push @foo, [ @bar ] if $self->{baz} eq $gamma; return \@foo; }

Except for the difference between

push @foo, [ @bar ];

and

push @foo, [ @bar ] if $self->{baz} eq $gamma;

... the two subroutines are identical. Which implies that I could and should refactor out the common code into an internal subroutine. I would like to have a structure like this:

sub alpha { my $self = shift; my $coderef = sub {1}; return $self->_internal_sub($coderef); } sub beta { my $self = shift; my $coderef = sub {$self->{baz} eq $gamma}; return $self->_internal_sub($coderef); } sub _internal_sub { my $self = shift; my $coderef = shift; ... foreach ... if ... if ... if ... push @foo, [ @bar ] if &$coderef; } } } else { push @foo, [ @bar ] if &$coderef; } } push @foo, [ @bar ] if &$coderef; return \@foo; }

... but of course the values of $self-baz and $gamma are unknown at the time of assignment to $coderef in beta().

I have a feeling I'm missing something obvious here. Can any of the monks suggest a remedy? Thanks.

Jim Keenan


In reply to 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.