Click here to get back home

Re: very strange bug (in perl?)

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Re: very strange bug (in perl?) Ben Bullock 03-19-2008
Posted by Ben Bullock on March 19, 2008, 9:19 pm
Please log in for more thread options
On Wed, 19 Mar 2008 16:41:19 -0700, dummy wrote:

> $ow = \@in;

The two lines like this cause the problem. When a program uses "my @in"
inside a loop, it creates a new @in each time it goes around the loop.
Usually if you have

my $something

inside a loop, Perl destroys it at the end of the loop, but here you have
taken a reference to it, so Perl keeps it.

However, when you put "my @in" outside the loop, the above reference
points to the same old @in each time, so \@in points to the same old
thing each time, hence your problem.

You can fix this by copying the whole @in array into another array:

                my @crud = @in;
$ow = \@crud;

But that may not be the optimal solution.

Posted by Gunnar Hjalmarsson on March 19, 2008, 9:37 pm
Please log in for more thread options
Ben Bullock wrote:
> when you put "my @in" outside the loop, the above reference
> points to the same old @in each time, so \@in points to the same old
> thing each time, hence your problem.
>
> You can fix this by copying the whole @in array into another array:
>
>                 my @crud = @in;
> $ow = \@crud;

Or simpler:

$ow = [ @in ];

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Posted by dummy on March 19, 2008, 9:51 pm
Please log in for more thread options
On Thu, 20 Mar 2008 02:37:04 +0100, Gunnar Hjalmarsson

>Ben Bullock wrote:
>> when you put "my @in" outside the loop, the above reference
>> points to the same old @in each time, so \@in points to the same old
>> thing each time, hence your problem.
>>
>> You can fix this by copying the whole @in array into another array:
>>
>>                 my @crud = @in;
>> $ow = \@crud;
>
>Or simpler:
>
> $ow = [ @in ];

Thanks again!

Posted by dummy on March 19, 2008, 10:23 pm
Please log in for more thread options
On Thu, 20 Mar 2008 02:37:04 +0100, Gunnar Hjalmarsson

>Ben Bullock wrote:
>> when you put "my @in" outside the loop, the above reference
>> points to the same old @in each time, so \@in points to the same old
>> thing each time, hence your problem.
>>
>> You can fix this by copying the whole @in array into another array:
>>
>>                 my @crud = @in;
>> $ow = \@crud;
>
>Or simpler:
>
> $ow = [ @in ];

If that same code was used in several places in the same scope would it
create more than one anonymous array?

Posted by Gunnar Hjalmarsson on March 20, 2008, 1:47 am
Please log in for more thread options
dummy@phony.info wrote:
> On Thu, 20 Mar 2008 02:37:04 +0100, Gunnar Hjalmarsson
>>
>> $ow = [ @in ];
>
> If that same code was used in several places in the same scope would it
> create more than one anonymous array?

Yes. Each occurrence of [ @in ] would _copy_ @in into a new anonymous
array, to which $ow would become a reference. (I'm assuming that the
value of $i would not be the same.)

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Similar ThreadsPosted
Re: very strange bug (in perl?) March 19, 2008, 8:02 pm
strange output of pack in perl 5.8.0 September 9, 2004, 7:36 pm
strange trailing output in PERL September 21, 2004, 2:30 pm
Perl 64 bit solaris - Strange Errors September 28, 2006, 10:10 pm
New to Perl - strange behaviour with backticks September 13, 2007, 5:08 pm
Apache+Perl Net::SMTP Strange problem May 16, 2007, 2:07 am
Strange Perl line : Return the result of a function to a function October 24, 2006, 9:19 am
strange February 14, 2005, 4:28 pm
This is strange!!! What happens in here??? January 12, 2007, 6:11 am
Strange DBI error January 13, 2005, 12:31 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap