<?xml version="1.0" encoding="windows-1252"?>
<node id="1006427" title="Re: Hashes Question" created="2012-11-30 03:36:01" updated="2012-11-30 03:36:01">
<type id="11">
note</type>
<author id="27571">
ColonelPanic</author>
<data>
<field name="doctext">
&lt;p&gt;Here is an example of how you could recurse through a structure of hash references in practice.&lt;/p&gt;
&lt;p&gt;It uses the &lt;code&gt;ref&lt;/code&gt; function to determine whether a given level is a  hash reference.&lt;/p&gt;
&lt;code&gt;
use Modern::Perl;

my %ref_chain = ( 
	'key1' =&gt; {
		'key2' =&gt; {
			'key3' =&gt; 'not a reference'	
		}	
	},
	'key4' =&gt; {
		'key5' =&gt; 'no ref here'	
	}	
);

sub traverse_hashes {
	my $hashref = shift;
	foreach my $key (keys %$hashref)
	{
		if (ref $hashref-&gt;{$key} eq 'HASH')
		{
			say "$key is a hash ref. Recursing.";
			traverse_hashes($hashref-&gt;{$key});	
		}
		else
		{
			say "$key is not a hash ref (value: $hashref-&gt;{$key})";
		}
	}	
}

traverse_hashes(\%ref_chain);
&lt;/code&gt;

&lt;div class="pmsig"&gt;&lt;div class="pmsig-27571"&gt;
&lt;br&gt;&lt;br&gt;When's the last time you used duct tape on a duct?  --Larry Wall
&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
1006411</field>
<field name="parent_node">
1006411</field>
</data>
</node>
