Re: [MacPerl-AnyPerl] question about "my" declarations

[email protected] (Bart Lateur) Fri, 28 Sep 2001 17:21:06 +0200
Newsgroups perl.macperl.anyperl
Organization MediaMind
Message-ID <[email protected]>
On Thu, 27 Sep 2001 09:30:19 -0700 (PDT), Tejasvini Prasad wrote:

>This code does not work correctly, but when I add a
>"my" before @temp it works fine. I understand that
>"my" declares a local variable....but I don't
>understand how this will affect the way values are
>assigned in my array. Any ideas?

Oops! Sorry for ignoring your main question.

If you don't declare the variable INSIDE THE LOOP, \@temp will be the
same reference every time you use it. So if you do

	@temp = ();
	push @ar, \@temp;

you eventually just get an array of all the same reference to the same
other array. 

	{  # loop body
	    my @temp;
	    push @ar, \@temp;
	}

creates a new array with a new reference, every time.

Note that if you move the declaration of @temp outside the loop, the
problem comes back. So it's not a "global vs. lexical" thing.

-- 
	Bart.