ericatkin has asked for the wisdom of the Perl Monks concerning the following question:
Hi,
My problem/confusion can be illustrated with the code below:
Eric
My problem/confusion can be illustrated with the code below:
On my system, I get:#!/usr/bin/perl use strict; use threads; use threads::shared; my %h:shared; for (0..10) { $h{$_}=&share({}); } for (keys(%h)) { print "$h{$_}\n"; }
Now, obviously, the addresses change with each invocation, but it is always 5 unique addresses that repeat. It seems to be creating 11 distinct hashes where I can file away my scalars, but it doesn't make sense to me that there would only be 5 unique memory addresses. Now, mysteries like this always bother me, but beyond the mystery, this behavior breaks XML::Dumper as it uses the memory address to ensure that a data structure that is referenced multiple times is only dumped once. Without the threads::shared module, I get 11 distinct memory locations as I would expect as illustrated below:HASH(0x9904d20) HASH(0x9904d38) HASH(0x9904d14) HASH(0x9904d2c) HASH(0x9904d08) HASH(0x9904d20) HASH(0x9904d38) HASH(0x9904d14) HASH(0x9904d2c) HASH(0x9904d08) HASH(0x9904d20)
Output:#!/usr/bin/perl use strict; my %h; for (0..10) { $h{$_}={}; } for (keys(%h)) { print "$h{$_}\n"; }
What am I missing? Is there a good reason for this behavior? It has rendered XML::Dumper useless for me.HASH(0x9ea67c8) HASH(0x9ea6720) HASH(0x9ea67b0) HASH(0x9ec8578) HASH(0x9ea66e4) HASH(0x9ea672c) HASH(0x9ea5b44) HASH(0x9ea69a8) HASH(0x9ea6600) HASH(0x9ec8590) HASH(0x9ea6990)
Eric
Back to
Seekers of Perl Wisdom