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, 11:26 pm
Please log in for more thread options
On Thu, 20 Mar 2008 02:37:04 +0100, Gunnar Hjalmarsson wrote:

>> You can fix this by copying the whole @in array into another array:
>>
>>                 my @crud = @in;
>> $ow = \@crud;

> Or simpler:
>
> $ow = [ @in ];

@} = @in;

My cruddy excuse for my original method is its benefit of being clearer
for a learner. As you point out it has the deficiency of creating an
unnecessary variable @crud instead of using an anonymous array.




Posted by dummy on March 19, 2008, 9:50 pm
Please log in for more thread options
On Thu, 20 Mar 2008 01:19:10 +0000 (UTC), Ben Bullock

>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.

Thank you for the explanation. I was able to fix it by moving the 'my'
declaration inside the loop, where it belonged, but I couldn't figure
out why that fixed it. I am HAPPY to find that there is no bug in perl.

Posted by Keith Keller on March 20, 2008, 12:50 am
Please log in for more thread options
>
> Thank you for the explanation. I was able to fix it by moving the 'my'
> declaration inside the loop, where it belonged, but I couldn't figure
> out why that fixed it. I am HAPPY to find that there is no bug in perl.

There are bugs in Perl, but if you'd read the Posting Guidelines, you'd
have realized that yours most likely was not.

--keith


--
kkeller-usenet@wombat.san-francisco.ca.us
(try just my userid to email me)
AOLSFAQ=http://www.therockgarden.ca/aolsfaq.txt
see X- headers for PGP signature information


Posted by dummy on March 20, 2008, 2:08 am
Please log in for more thread options
On Wed, 19 Mar 2008 21:50:54 -0700, Keith Keller

>>
>> Thank you for the explanation. I was able to fix it by moving the 'my'
>> declaration inside the loop, where it belonged, but I couldn't figure
>> out why that fixed it. I am HAPPY to find that there is no bug in perl.
>
>There are bugs in Perl, but if you'd read the Posting Guidelines, you'd
>have realized that yours most likely was not.
>
>--keith

Well, I did quite a bit of reading and sweating before posting, but was
still puzzled. I admit I shouldn't have used the language I did.

But I _did_ learn quite a bit from this exchange.

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