in reply to Re^3: Warnings on unused variables?
in thread Warnings on unused variables?
For example:
sub foo1 { my ($c, $d, $e, $f) = @_; return $d * f; } sub foo2 { my ($c, $d, $e, $f) = @_; return $c + $d + $e + $f; } sub do_foo { my $which = $_[0] ? \&foo1, \&foo2; $which->(1, 2, 3, 4); }
As you see foo1 uses only uses the second argument and fourth ($d, $f). But the assignment happens via my ($c, $d, $e, $f) = @_;. If a warnings is introduced for lexical variables that are used once then it will warn. (lexicals $c and $e are declared and initialized but never used.
Yes, the assignment could be changed into: my ($d, $f) = @_[1, 3]; but this would be worse from a maintenance point of view.
With my ($c, $d, $e, $f) = @_; you know exactly what parameters are passed (assuming they get a decent name). If you use my ($d, $f) = @_[1, 3]; then you are clueless about the other arguments.
If you later need the other arguments then you need to start looking in what order they were passed.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Warnings on unused variables?
by AZed (Monk) on Oct 02, 2008 at 01:18 UTC | |
by Animator (Hermit) on Oct 02, 2008 at 07:08 UTC | |
by JavaFan (Canon) on Oct 02, 2008 at 16:15 UTC | |
by AZed (Monk) on Oct 02, 2008 at 16:00 UTC |