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'
In reply to Re: difference between my and local
by Perl Mouse
in thread difference between my and local
by jeanluca
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |