Excellent! I guess this refactoring pattern maps nicely to TheDamian's Always unpack @_ first best practice (PBP, p. 178).

Update: Actually, after thinking about it a bit, it doesn't map exactly. Consider:

my $array_ref = [ qw( 1 4 9 10 15 23 34 84 100 ) ]; add_n_to_values_and_print( $array_ref, 15 ); sub add_n_to_values_and_print{ my ( $list, $add ) = @_; for my $value ( @$list ){ $value += $add; print $value; } }

This follows Mr. Conway's admonition to unpack @_ but it doesn't accomplish the goal of avoding changes to the reference passed in that Fowler is after. What we really need to do is:

my $array_ref = [ qw( 1 4 9 10 15 23 34 84 100 ) ]; add_n_to_values_and_print( $array_ref, 15 ); sub add_n_to_values_and_print{ my ( $list, $add ) = @_; # dereference and work with a local copy of the array my @local_list = @$list; for my $value ( @local_list ){ $value += $add; print $value; } }

Truthfully, that's not a very good example, as I wouldn't generally write code that actually updates the value I would probably write print $value + $add, but for the sake of an example...


In reply to Re^4: Refactoring Perl #7 - Remove Assignments to Parameters by agianni
in thread Refactoring Perl #7 - Remove Assignments to Parameters 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.