M
Mark Keightley
Hi
I'm currently writing a script to read windows style config files. You
know the sort of file that reads like this
[section name]
key1 = value1
key2 = value2
[another section name]
....
The config is then held in a hash of hashes. The problem I'm having is
getting the hash with the key value pairs stored in the main config
hash. I've written a sub routine that reads the key value pairs and
then returns a reference to this hash. The problem is that because
there is a reference back to this hash, even with it being a local hash
(created with my) to that sub, it never goes out of scope. Hence only
the first section of the main config hash is correct. Section 2 has
keys and values for both sections 1 and 2, section 3 has 1,2 and 3 etc.
One way around this that I tried was de-reference the reference in to a
hash take a reference to this new hash and then make the first reference
undef. The code I used was like this
$href1 = \%section;
$href2 = {%{$href1}};
$href1 = undef;
return $href2;
But this still failed in the same way. It looks like rather than the
'chain' of references I expected that all references point back to the
original data. So to my question how do I copy data using references,
but keep the copies detached from each other?
Thanks in advance
I'm currently writing a script to read windows style config files. You
know the sort of file that reads like this
[section name]
key1 = value1
key2 = value2
[another section name]
....
The config is then held in a hash of hashes. The problem I'm having is
getting the hash with the key value pairs stored in the main config
hash. I've written a sub routine that reads the key value pairs and
then returns a reference to this hash. The problem is that because
there is a reference back to this hash, even with it being a local hash
(created with my) to that sub, it never goes out of scope. Hence only
the first section of the main config hash is correct. Section 2 has
keys and values for both sections 1 and 2, section 3 has 1,2 and 3 etc.
One way around this that I tried was de-reference the reference in to a
hash take a reference to this new hash and then make the first reference
undef. The code I used was like this
$href1 = \%section;
$href2 = {%{$href1}};
$href1 = undef;
return $href2;
But this still failed in the same way. It looks like rather than the
'chain' of references I expected that all references point back to the
original data. So to my question how do I copy data using references,
but keep the copies detached from each other?
Thanks in advance