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

Dear Masters,
I'm trying to print out a directory name from a method generated by Net::Google
print Dumper $re->directoryCategory(); # which gives $VAR1 = bless( { 'fullViewableName' => 'foo', 'specialEncoding' => '' }, 'DirectoryCategory' );
My problem is how can I print out the value of "fullViewableName" under the value above? I tried this:
foreach my $dc ( %{ $re->directoryCategory() } ) { print $dc->fullViewableName()."\n" ; }
But doesn't work. What's the correct way to do it?
My complete code is here:
use warnings; use strict; use Data::Dumper; use Net::Google; use constant LOCAL_GOOGLE_KEY =>"***********************"; my $service = Net::Google->new( key => LOCAL_GOOGLE_KEY ); my $session = $service->search(); $session->query(qw (beadwork )); $session->starts_at(5); $session->max_results(15); my $responses = $session->response(); #print Dumper $responses ; foreach my $r ( @{$responses} ) { print sprintf( "%s : %s\n", $r->searchQuery(), $r->estimatedTotalResultsCount() ); foreach my $re ( @{ $r->resultElements() } ) { print $re->URL() . "\n"; print Dumper $re->directoryCategory(); foreach my $dc ( %{ $re->directoryCategory() } ) { print $dc->fullViewableName()."\n"; } } }
Update: I tried putting 'keys' before the hash reference as suggested by BerntB, but won't do because this is a blessed hash.

---
neversaint and everlastingly indebted.......

Replies are listed 'Best First'.
Re: Accessing a blessed hash reference - Google API
by BerntB (Deacon) on Apr 10, 2006 at 06:14 UTC
    foreach my $dc ( %{ $re->directoryCategory() } ) {
    Uh, you haven't just forgotten:
    foreach my $dc (keys %{ $re->directoryCategory() } ) {
    Update:
    Check with ref() and reftype() that it really is a hash... :-)

    This works for me:
    perl -e '$a={1,2,3,4}; bless $a,"foo"; print "$_\n" foreach (keys %$a);'

Re: Accessing a blessed hash reference - Google API
by japhy (Canon) on Apr 10, 2006 at 13:34 UTC
    $re->directoryCategory()->{fullViewableName} should work for you. $re->directoryCategory is returning a blessed hash reference, yes, but do you know if there are any object methods associated with it at all? Unless there are, you'll have to do it the way I showed, accessing a hash key manually.

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
      Dear Japhy,
      Thanks a lot. Works just nice!

      ---
      neversaint and everlastingly indebted.......