<#5 - Introduce Explaining Variable Series Index #7 - Remove Assignments to Parameters >

You have a temporary variable assigned to more than once, but is not a loop variable nor a collecting temporary variable.

Make a separate temporary variable for each assignment.

(Fowler, p. 128)

This is a refactoring of a common anti-pattern: the inappropriately re-used temporary variable. In Perl, the refactoring looks like this:
sub print_perimeter_and_area{ my $self = shift; my $temp = 2 * ( $self->{_height} + $self->{_width} ); print "$temp\n"; $temp = $self->{_height} * $self->{_width}; print "$temp\n"; }

becomes:

sub print_perimeter_and_area{ my $self = shift; my $perimeter = 2 * ( $self->{_height} + $self->{_width} ); print "$perimeter\n"; my $area = $self->{_height} * $self->{_width}; print "$area\n"; }

Get the code

Not much of note here, just keep in mind Fowler's exceptions: loop variables or collecting variables are all good. Generally, I don't find loop variables to be all that useful in Perl, and I don't know that I would reuse one anyway. I imagine that Fowler is alluding to something like:
my $i; for $i ( 1..$some_max ){ #do something } # and later for $i ( 1..$some_other_max ){ #do something else }

but I don't generally find myself doing that anyway. True, loop variables are rarely going to get you in trouble, as they are usually initialized on each iteration, but it certainly doesn't make the code particularly readable. As such, I'd rather name my loop variables based on what they're actually doing. But I also like $verbose_variable_names, so YMMV.

The upshot is that this refactoring pattern is as much about making your code understandable as it is about avoiding errors that can occur when you reuse a temp variable. If you forget to reinitialize it before the second time you use it (or, say, accidentally delete the line that does), you're in trouble.

perl -e 'split//,q{john hurl, pest caretaker}and(map{print @_[$_]}(joi +n(q{},map{sprintf(qq{%010u},$_)}(2**2*307*4993,5*101*641*5261,7*59*79 +*36997,13*17*71*45131,3**2*67*89*167*181))=~/\d{2}/g));'

In reply to Refactoring Perl #6 - Split Temporary Variable by agianni

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.