0

I could never clearly understand the use of local in perl except that anything defined local within a stack will be different from other existing my variables and local variable will be visible within other subroutines too.

AgA
  • 2,078
  • 7
  • 33
  • 62
  • 3
    Related: [What is the difference between `my` and `local` in Perl?](http://stackoverflow.com/questions/129607/what-is-the-difference-between-my-and-local-in-perl) which links to required reading [Seven Useful Uses of `local`](http://perl.plover.com/local.html). – daxim Mar 28 '11 at 12:51

2 Answers2

4

From perldoc perlsub:

A local just gives temporary values to global (meaning package) variables. It does not create a local variable. This is known as dynamic scoping. Lexical scoping is done with my, which works more like C's auto declarations.

Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
  • `local` is a leftover from Perl 4, which didn't have `my` or `our`. All variables were package variables and there was no lexical scope. Perl 5 is much better. – shawnhcorey Mar 28 '11 at 15:16
1

local was the means by which the scope of variables could be reduced before my was introduced to the language. It's pretty much only used now in situations where lexical (my) variables can't be used.

ikegami
  • 367,544
  • 15
  • 269
  • 518