in reply to Re: hash reference syntax?
in thread hash reference syntax?

Thanks for both of your answers. My error was that I was testing only against 'HASH' while I should have given myself an out. Taking your answers a step further, when I apply all types mentioned for ref() at Perldoc.com as in the following:
#!/usr/bin/perl use strict; sub traverse($); #my $hash = { # key0 => 'value0', # key1 => { key2 => 'value2' }, # key3 => { key4 => 'value4', key5 => { key6 => 'value6' } } #}; my %hash; $hash{key0} = 'value0'; $hash{key1}{key2} = 'value2'; $hash{key3}{key4} = 'value4'; $hash{key3}{key5}{key6} = 'value6'; traverse(\%hash); sub traverse($) { my $r = shift; if (ref($r) eq 'HASH') { for my $v (values %$r) { traverse($v); } } elsif (ref($r) eq 'SCALAR') { print "SCALAR:\t'$r'\n"; } elsif (ref($r) eq 'ARRAY') { print "ARRAY:\t'$r'\n"; } elsif (ref($r) eq 'REF') { print "REF:\t'$r'\n"; } elsif (ref($r) eq 'LVALUE') { print "LVALUE:\t'$r'\n"; } elsif (ref($r) eq 'CODE') { print "CODE:\t'$r'\n"; } elsif (ref($r) eq 'GLOB') { print "GLOB:\t'$r'\n"; } else { print "unexpected value:\t'$r'\n" } }

I get the following unexpected (at least to me...) results:

unexpected value: 'value2' unexpected value: 'value0' unexpected value: 'value6' unexpected value: 'value4'
How is this possible if I have accounted for all possible types? Thanks.

Replies are listed 'Best First'.
Re: Re: Re: hash reference syntax?
by tachyon (Chancellor) on Mar 06, 2004 at 01:46 UTC

    How is this possible if I have accounted for all possible types?

    'some string' is a literal. It is not a reference to anything. I presume you though that ref() would report scalar.

    $foo = 'bar'; print '$foo is a ', ref($foo), $/; print '\$foo is a ', ref(\$foo), $/; __DATA__ $foo is a \$foo is a SCALAR

    cheers

    tachyon

      I did. Huh. Wow. Thanks.