The basic difference is that, when you declare $foo = 3; and then
you go into a block or sub and say local $foo = 5; the value in $foo
inside the blocks or sub is 5, but outside is 3 here is some code:
#!/usr/bin/perl -w
#didn't use strict, to make the example clearer.
$foo = 3; #default value of $foo
{
local $foo = 5; #now the value of $foo is 5 while inside the braces
print "$foo\n"; # prints 5.
}
print "$foo\n"; # prints 3.
With my is a bit different code examples:
#!/usr/bin/perl -w
use strict; #<- very important.
my $foo = 3; # $foo has a value of 3, and is seen by the compiler.
{
my $bar = 5; # $ bar life is limited to the block.
print "$bar\n"; # you get 5.
print "$foo\n"; # you get 3.
}
print "$bar\n"; # <- compiler gives error.$bar does not exsist (kind o
+f).
print "$foo\n"; # you get 3.
I hope this clear, things up a bit, and i hope i'm not completely wrong
if i am i'm sure the other monks, will point it out.
monk
Update:
s/compiler/interpreter/ig;
monk
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.