Since it has been suggested that you use strict and avoid symbolic references, the inevitable question will be "then how do I do what I'm trying to do here?" The answer is almost always to use a hash. Here's a quick example of your code ported to use a lexically scoped hash:
my $n = 0; my %nums; foreach ( qw( zero one two three ) ) { $nums{$_} = $n++ || "zero"; } my $msg = ""; foreach ( qw( zero one two three ) ) { $msg .= "$_ = '$nums{$_}'\n"; } print $msg; print qq( zero = '$nums{zero}' one = '$nums{one}' two = '$nums{two}' three = '$nums{three}' );
You get the benefit of being able to dynamically create entries in the hash, without the side-effect of possibly clobbering unrelated variables.
In reply to Re: when is "my" not necessary?
by revdiablo
in thread when is "my" not necessary?
by argv
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |