The difference is between dynamic and static
scope. 'my' behaves as the auto variables of
C/C++ and most other languages. 'local', on the
other way, behaves as bind variables in Lisp.
The real difference can be seen when you have
several scopes.
Suppose you have the following code:
our $a = 3;
sub f {
print "$a\n";
}
sub g {
my $a = 7;
print "$a\n";
&f ();
print "$a\n";
}
&g ();
print "$a\n";
you would get: 7 - 3 - 7 - 3
If you substitute the my in 'g' by local, you get:
7 - 7 - 7 - 3
Internally, the difference in the implementation
is that local stores the variable value in a
stack for the duration of the scope, and restores
it at the end of the scope, while my actually
creates a new variable which hides the outer one
for the duration of the scope where it is defined,
but not for other scopes which may be invoked.
creates
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.