Did you try this? Generally, "$hash->{shift}" is interpreted as "$hash->{'shift'}", but I think you mean "$hash->{shift()}".
Also, this and other solutions I've seen here seem to ignore the possibility of running into a non-hash somewhere along the way.
use Test::More 'tests' => 5;
sub deep_exists {
my $hash = shift;
my $key = shift;
return 0 if ( ref $hash ne ref {} || ! exists $hash->{$key} );
return deep_exists( $hash->{$key}, @_ ) if @_;
return 1;
}
my %h;
$h{there}{also}{yes} = 1;
$h{hash}{shift}{scalar} = 1;
ok( deep_exists( \%h, 'there', 'also', 'yes' ), 'there also yes' );
ok( deep_exists( \%h, 'there', 'also' ), 'there also' );
ok( ! deep_exists( \%h, 'there', 'also', 'no' ), 'there also no' );
ok( deep_exists( \%h, 'hash', 'shift', 'scalar' ), 'hash shift scalar'
+ );
ok( ! deep_exists( \%h, 'hash', 'shift', 'scalar', 'hash' ),
'hash shift scalar hash' );
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.