in reply to my and local differences
That has the advantage that every knows it's a constant and not subject to change. Of course, if all you are doing is documenting, this may not be an option.use constant EB => "\034";
I have strict in use all the time, but I have only ever used my(), so I'm not clear on the difference.local pushes the current copy of the variables value (if any) onto a stack and assigns a new value which is available to everything within the original scope of local. When that variable passes out of scope, the original value is pulled from the stack and restored. In practice, local is rarely needed, unless my cannot be used.
Global punctuation variables, for example, cannot be declared lexically with my and therefore must use local if you wish to constrain their scope.
The above snippet uses "slurp mode" to read the entire contents of a file into a scalar. local is in a block to ensure that the change to $/ does not affect other portions of the program (since $/ is global).open FILE, $somefile or die $!; { local $/; # slurp mode $foo = <FILE>; } close FILE;
Cheers,
Ovid
Join the Perlmonks Setiathome Group or just go the the link and check out our stats.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
RE: (Ovid) Re: my and local differences
by HaB (Sexton) on Nov 10, 2000 at 23:29 UTC |