On Sun, 5 May 2002, Jon wrote:

> I thought that using 'local' would successfully scope those globals to
> within a sub, so you could,k for example, slurp an entire file by doing:
> 
> local $/ = undef;
> my $file = <FH>;
> 
> Or am I wrong in that?  I use it frequently, and don't seem to have any
> troubles.

Okay, local takes a temporary copy of the variable and then restores the
original value at the end of the block.  It's the same variable though. my
creates a new variable that is only visible from within the block and
can't be seen from other subroutines.  With my there are two variables 
that just happen to be called the same things, and which one you get 
depends if you're in within the enclosing block.

The difference is that when you use local any subroutines that you call
from within that subroutine will *also* see the variable.  With a 
my variable whatever you change can't be seen in other subroutines

The practical difference is if you set $/ and you call any routine from
within that same subroutine (that same block) they will also see the new 
version of $/.  This means that anything that you call from within that 
routine will also slurp in the whole file when it tries to read a line - 
if it was expecting it or not.

In terms of mod_perl this isn't so bad.  The local is fine when used in a 
subroutine like you described, as this subroutine will be exited by the 
time the handler returns, meaning you won't get any interaction between 
calls.

Of course there's still ways for you to screw yourself over with local, 
but used carefully it should be fine...

Later.

Mark.

-- 
s''  Mark Fowler                                     London.pm   Bath.pm
     http://www.twoshortplanks.com/              [EMAIL PROTECTED]
';use Term'Cap;$t=Tgetent Term'Cap{};print$t->Tputs(cl);for$w(split/  +/
){for(0..30){$|=print$t->Tgoto(cm,$_,$y)." $w";select$k,$k,$k,.03}$y+=2}

Reply via email to