develooper Front page | perl.perl5.porters | Postings from May 2007

localising hash element by variable

Thread Next
From:
hv
Date:
May 26, 2007 18:02
Subject:
localising hash element by variable
Message ID:
200705261334.l4QDYHbP002732@zen.crypt.org
It seems inconceivable to me that so simple an example could be a bug
in perl, and yet I cannot square the behaviour with my expectations.
What am I missing?

The testcase that follows fails at least on 5.8.8 and blead@31281:

#!/usr/bin/perl -w
use strict;
my %v = (2 => 3);
my $el;
{ 
    $el = 2;
    local $v{$el} = 4;
    $el = 17;
}
use Data::Dumper; print Dumper(\%v);
if ($v{2} == 3 && !$v{17}) {
    # This is what I expected.
    print "ok\n";
} elsif ($v{2} == 4 && $v{17} == 3) {
    # This is what I see.
    print "not ok\n";
} else {
    # Hmm, you got yet another result.
    die "# whoa, freaky\n";
}
__END__


If I am missing something stupid, my apologies: the source of the test case
arose as a variation from my usual coding style, so I can believe I might
not have come across this behaviour before. However I cannot see anything
in perlsub 'Temporary Values via local()' to determine it either way, which
should at least be remedied.

Hugo

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About