in reply to Re^2: Refactoring Perl #7 - Remove Assignments to Parameters
in thread Refactoring Perl #7 - Remove Assignments to Parameters
by_val does not modify its parameter ($_[0]), it modifies a copy of its parameter ($var). Without realizing it, you've used the very refactoring tool you are trying to present to us (my $var = shift;).
Using a temporary variable instead of assigning to (or even reading from) a parameter is standard practice in Perl!
Update: Example:
sub inc_original { $_[0]++; return $_[0]; } sub inc_refactored { my ($var) = @_; $var++; return $var; } { my $i = 3; my $j = inc_original($i); print("$i + 1 = $j\n"); # 4 + 1 = 4 } { my $i = 3; my $j = inc_refactored($i); print("$i + 1 = $j\n"); # 3 + 1 = 4 }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Refactoring Perl #7 - Remove Assignments to Parameters
by agianni (Hermit) on Aug 23, 2007 at 15:57 UTC |