bulrush has asked for the wisdom of the Perl Monks concerning the following question:

I'm not the greatest at hash refs, but I'm trying to find the subkeys of a DBM::Deep key. My program will help explain.
# Using Strawberry Perl 5.18.2

use strict;
use DBM::Deep;

my($dbm,$i,$j,$s,$t);
my(@k);

$dbm=DBM::Deep->new('test.db');

$dbm->{'A'}->{'20150301'}=10; # First quote for A
$dbm->{'A'}->{'20150302'}=11; # First quote for A
$dbm->{'B'}->{'20150301'}=8; # First quote for A
$dbm->{'B'}->{'20150302'}=8.5; # First quote for A

# How to list only subkeys for A?
@k=keys(%$dbm->{'A'}); # ERROR HERE
# ERROR IS: "Using a hash as a reference is deprecated at testkey.pl line 14.
# Type of argument to keys on reference must be unblessed hashref or
# arrayref at testkey line 14."
$i=($#k+1);
print "$i keys for key A\n";


exit; # Main
#############################################################

I tried looking in the docs but didn't see any examples of what I wanted. '

Thank you!

  • Comment on Trying to find subkeys of a DBM::Deep key

Replies are listed 'Best First'.
Re: Trying to find subkeys of a DBM::Deep key
by toolic (Bishop) on Mar 10, 2015 at 15:09 UTC
    This style of dereferencing works for me:
    use warnings; use strict; my $dbm; $dbm->{'A'}->{'20150301'}=10; # First quote for A $dbm->{'A'}->{'20150302'}=11; # First quote for A $dbm->{'B'}->{'20150301'}=8; # First quote for A $dbm->{'B'}->{'20150302'}=8.5; # First quote for A my @k = keys %{ $dbm->{A} }; use Data::Dumper; print Dumper(\@k); __END__ $VAR1 = [ '20150302', '20150301' ];
      Yep, it works. Thanks! This also works:
      @k=keys( %{$dbm->{'A'}} ); # With single quotes around key
      
Re: Trying to find subkeys of a DBM::Deep key
by MidLifeXis (Monsignor) on Mar 10, 2015 at 17:04 UTC

    The reason that @k=keys(%$dbm->{'A'}); fails and @k=keys(%{ $dbm->{'A'} }); succeeds is because the %{...} applies to the entire $dbm->{A}, and not just the $dbm part.

    --MidLifeXis