in reply to difference between my and local
my creates a new variable which is only valid inside a sub or blockYes, to be more accurate: my creates a variable that exists till the end of the innermost enclosing block.
local uses the global var and saves its original value and you can give it a new value which is also only valid inside a sub or blockYes, and the value exists till the innermost enclosing block is exited.
So..... both create a new variable.How did you draw that conclusion? In your description of local you say it uses the global variable, and saves its original value, giving you a new value. That's correct - it uses the global variable.
The following is an easy reminder of the difference:
my creates a new variable; local creates a new value.
Note also that a myed variable isn't visible outside the block it's declared in, while a localized value is:
#!/usr/bin/perl use strict; use warnings; use vars '$global'; my $lexical; $global = 'outer'; $lexical = 'outer'; sub show { printf "\$global is '%s'; \$lexical is '%s'\n", $global, $lexical; } show; { local $global = 'inner'; my $lexical = 'inner'; show; } __END__ $global is 'outer'; $lexical is 'outer' $global is 'inner'; $lexical is 'outer'
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: difference between my and local
by jeanluca (Deacon) on Nov 15, 2005 at 13:48 UTC | |
by blazar (Canon) on Nov 15, 2005 at 13:57 UTC | |
by Perl Mouse (Chaplain) on Nov 15, 2005 at 14:36 UTC | |
by blazar (Canon) on Nov 15, 2005 at 16:25 UTC | |
|
Re^2: difference between my and local
by merlyn (Sage) on Nov 15, 2005 at 17:05 UTC |