Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Object get method as variable name

by danmcb (Monk)
on Jan 09, 2013 at 19:31 UTC ( [id://1012550]=perlquestion: print w/replies, xml ) Need Help??

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

I am using Net::Google::Analytics to grab stats from the goog.

The names of object accessors in the Row object are generated at runtime according to the parameters requested. So if you request the ga:pageviews metric, the row will have a method called get_pageview.

My code:

my @dimensions = qw/ page_path /; my @metrics = qw/ pageviews unique_pageviews avg_time_on_page entrance +s exits /; . . . (get the data into $res, this works fine) foreach my $r (@{ $res->rows }){ my @line; foreach my $get ( @dimensions, @metrics ){ my $get_method = "get_$get"; push @line, $r->{$get_method}->(); # this is wrong } push @report, join( "\t", @line); }

The difficulty I have is calling the method name. I get a "Not a HASH reference at ..." error.

Can you help please, oh holy ones? ;-)

Thanks!

Replies are listed 'Best First'.
Re: Object get method as variable name
by blue_cowdawg (Monsignor) on Jan 09, 2013 at 19:39 UTC

    Consider the following:

    #!/usr/bin/perl -w use strict; my $s = new speak(); foreach my $critter(qw/ cat dog spider /){ $s->$critter(); } exit(0); package speak; sub new { bless {},"speak"; } sub cat { print "meow!\n"; } sub dog { print "woof!\n"; } sub spider { print "arf!\n"; } 1;
    which may give you some enlightenment that you seek.

    When run it yields:

    $ ./doit.pl meow! woof! arf!
    Hope this is of some help.


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
Re: Object get method as variable name
by roboticus (Chancellor) on Jan 09, 2013 at 19:34 UTC

    danmcb:

    It appears that it's complaining that $r isn't a hashref. So use Data::Dumper or something and find out what it *is*.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1012550]
Approved by blue_cowdawg
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-03-29 05:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found