O
Ozgun Erdogan
As a newbie to Perl programming, I've realized that hv_iterinit has
side effects. That is, if you want to walk over a hash structure,
there is no way you can walk over it without changing the internal
values of the hash itself. And hashes in Perl are primitive data
types.
So, if you want to read the values in a hash, there is no way for you
to do it without modifying other values in the hash itself. From my
perspective (and from my perspective only), this is some what
equivalent to:
$x = 4;
Print $x;
(now $x is 10).
I know it sounds exaggerated, and probably people discussed for a
while and thought it'd bite no one to implement it like this, but
here's where this unexpected behavior hurts:
Take CPAN module Data::Structure::Util, which detects circular
references. This is something which I really need, but goes into
infinite recursion when we have:
hash1 has a weak reference weak1 to hash2. hash2 has a hard reference
hard1 pointing back to hash1. The code goes into infinite recursion
because hv_iterinit would set xhv_riter to 0 for hash1 every time. I
think this is not a bug in Data::Structure::Util, it's a bug in Perl!
You should be able to walk over a primitive type without changing its
value, especially in a widely used language as Perl.
Are there any other Perl 5 API functions available that'd let me walk
over a hash without modifying the hash itself?
Ozgun.
side effects. That is, if you want to walk over a hash structure,
there is no way you can walk over it without changing the internal
values of the hash itself. And hashes in Perl are primitive data
types.
So, if you want to read the values in a hash, there is no way for you
to do it without modifying other values in the hash itself. From my
perspective (and from my perspective only), this is some what
equivalent to:
$x = 4;
Print $x;
(now $x is 10).
I know it sounds exaggerated, and probably people discussed for a
while and thought it'd bite no one to implement it like this, but
here's where this unexpected behavior hurts:
Take CPAN module Data::Structure::Util, which detects circular
references. This is something which I really need, but goes into
infinite recursion when we have:
hash1 has a weak reference weak1 to hash2. hash2 has a hard reference
hard1 pointing back to hash1. The code goes into infinite recursion
because hv_iterinit would set xhv_riter to 0 for hash1 every time. I
think this is not a bug in Data::Structure::Util, it's a bug in Perl!
You should be able to walk over a primitive type without changing its
value, especially in a widely used language as Perl.
Are there any other Perl 5 API functions available that'd let me walk
over a hash without modifying the hash itself?
Ozgun.