Can anyone shed light on this problem?

I am using ActiveState's Perl Build 522 on Windows 2000.

I noticed some strange behaviour with a hash whilst writing a subroutine.

What happens is that if I pass a hash by reference to a subroutine, traverse
that hash using a while() loop, and return in the middle of the loop, the
hash is corrupted in some way.

I have written some example code that reproduces this same behaviour (see
below):

Run this from the command line:

Set the $extra_loop variable to 0 or non-0 and observe the different
behaviour.

----------------------------------------------------------------------------
-----------------------
#!/usr/bin/perl

        use Data::Dumper;

        print "\n";
        print "A Problem With Hash References?\n";
        print "\n";
        print "Try this with \$extra_loop set to 0 or non-0\n";
        print "\n";

        $extra_loop = 1;


        $hash =
        {
                'key0'  =>      'value0',
                'key1'  =>      'value1',
                'key2'  =>      'value2',
                'key3'  =>      'value3',
        };


        if( $extra_loop )
        {
                foreach my $key ( keys %{$hash} ) { my $value = ${$hash}{$key}; }
        }

        print "\n";
        print Dumper( $hash );
        print "\n";


        test( $hash );


        if( $extra_loop )
        {
                foreach my $key ( keys %{$hash} ) { my $value = ${$hash}{$key}; }
        }

        print "\n";
        print Dumper( $hash );
        print "\n";

        exit;

sub test
{
        while( my( $key, $value ) = each %{$hash} )
        {
                if( $value eq 'value1' )
                {
                        return;
                }
        }
}
----------------------------------------------------------------------------
-----------------------

Is this a 522 problem? A Win2k problem? A general Perl problem?

I would appreciate any thoughts and comments.

Thanks,

Chris Humphries


_______________________________________________
ActivePerl mailing list
[EMAIL PROTECTED]
http://listserv.ActiveState.com/mailman/listinfo/activeperl

Reply via email to