Hi all,

I seem to have completely confused myself with a data structure, as
testing for a keys existence seems to create an anonymous hash ref. The
following is a very cut down version of my code, to try and explore
what's going on.

#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper;

my $refer = { 'value', { 'NetworkServices', {'value', '1'}}};

print Dumper($refer);

my $test1 = ${refer}->{value}->{fred};
print Dumper($refer);
#this doesn't change the data as i'd expect

my $test2 = ${refer}->{value}->{fred}->{value};
print Dumper($refer);
#this actually creates a key 'fred' pointing to a empty hash ref
#I also tried 
if ( ${refer}->{value}->{fred}->{value} ) { #do stuff, which also change
the structure }
#this also changes the date. 


So my question really has two parts. 

Why does the above actually change the data, any pointers to reading
material would be great. I've read a bit about autovification, is that
what's going on. If so, how is this happening with an "assignment" or an
"if".

Given that I have a data structure similar to the above (returned from a
module), of which I don't know the exact shape. How do I test for the
existence of a key, down a branch without changing the data. All I could
come up with is:

if (${refer}->{value}->{fred} ) {
        my $test3 = ${refer}->{value}->{fred}->{value}
}

As I know if fred exists there will be a key called value, and testing
for fred doesn't change my data. Is there a better way of doing this?

Thanks for any pointers.

Brian 



http://www.bbc.co.uk/ - World Wide Wonderland

This e-mail (and any attachments) is confidential and may contain
personal views which are not the views of the BBC unless specifically
stated.
If you have received it in error, please delete it from your system. 
Do not use, copy or disclose the information in any way nor act in
reliance on it and notify the sender immediately. Please note that the
BBC monitors e-mails sent or received. 
Further communication will signify your consent to this.

Reply via email to