Both local @recArray; and my @recArray; will give you an empty array to start with, but the latter is considered better because it has lexical scope, while the former has dynamic scope. This means that, for example, in
...calling the function baz() has the effect of changing the value of $foo[ 0 ] inside the block; in contrast, the lexical variable @bar remains unaffected by what happens in baz().our @foo = ( 0 ); my @bar = ( 'a' ); { local @foo = ( 1 ); my @bar = ( 'b' ); baz(); print "$foo[ 0 ] $bar[ 0 ]\n"; # may not print "1 b" } sub baz { $foo[ 0 ] = 3; $bar[ 0 ] = 'x'; } __END__ 3 b
By the way, the scoping behavior described above is not specific to arrays; it applies equally to hashes, scalars, and typeglobs as well.
the lowliest monk
In reply to Re: Proper way to initialize local variables
by tlm
in thread Proper way to initialize local variables
by rzward
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |