Answer 1
I would use the second variation, because it hides $foo from the rest of the code, eliminating the possibility of side-effects from having $foo still visible. The code generated by this Perl reuses your scoped $foo anyway as an optimization, so there is no penalty.
Note that my ($foo); could be more clearly represented as my $foo; - the parens are usually used in declarations to collapse several lines into one, such as:
ormy ($a, $b, $c);
or to capture a subroutine's argumentsmy ($a, $b, $c) = (0, 1, 2);
my ($a, $b) = @_;
Answer 2
The declaration of $key and $value both outside and inside the loop is redundant, but I see nothing wrong with your code inside the loop, other than the fact that the loop control variable $i is not referenced in the body, so the loop could be rewritten as:
Again, the scoped variables are going to be reused.for (1 .. $a_whole_lot) { my ($key, $value) = returnsAnArray (); }
Aside
In question 1, your code doesn't even need a $foo.
In general, you are using the C-style for loop, and it would be more Perlish of you to use for or foreach as shown.codethatUsesFoo (getValue()) for 1 .. $a_huge_value;
Hope this helps.
In reply to Re: Two Questions on "my"
by pbeckingham
in thread Two Questions on "my"
by C_T
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |