Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: my and local??

by monk (Scribe)
on Jul 28, 2000 at 16:32 UTC ( [id://24836]=note: print w/replies, xml ) Need Help??


in reply to my and local??

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://24836]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-24 05:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found