> Can anyone explain the difference between my() and
> local() ?

Yes

> The manual says that I usually want to use my() but
> what exactly happens to a variable using local?

my() creates a brand new variable, which is undef unless
you specify otherwise.  It is only visible within the
current lexical scope.  E.g.

my $x = "World!";
foreach (1..1) {
  my $x = "Hello";
  print $x;
}
print " $x";

should print "Hello World!";

since my is lexically scoped, when you leave the foreach
you lose that variable.

local() is for creating a temporary copy of a built in
variable.  This is useful for this kind of thing:

local @ARGV = ("filename");
print "$.: $_" while (<>);

which will make a temporary copy of @ARGV; assign
"filename" to it.  And then the next line iterates over
that file, printing line numbers.  When you leave the scope
the old value of @ARGV is restored.

Jonathan Paton

__________________________________________________
Do You Yahoo!?
Everything you'll ever need on one web page
from News and Sport to Email and Music Charts
http://uk.my.yahoo.com

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to