in reply to Scope Difference
in thread I need a simple explantion of the difference between my() and local()

A good use of local is when you are tweaking a special variable.

Let's say you want to slurp a whole file into a scalar. Your code would look something like this:

my $file; { local $/; open (FILE, "<$foo") or die "Unable to open $foo: $!"; $file = <FILE>; close FILE; }

By enclosing the modification of $/ in a block, you avoid surprises down the road. $/ only has its value changed within that block.


TGI says moo

Replies are listed 'Best First'.
Re: Re: Scope Difference
by dws (Chancellor) on May 02, 2001 at 22:18 UTC
    By narrowing the block that holds the local, you can further contain the effect.
    open(FILE, "<$foo") or die "$foo: $!"; my $file = do { local $/; <FILE> }; close(FILE);