Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Parsing a Hash

by northwestdev (Acolyte)
on Jul 21, 2009 at 20:31 UTC ( #782074=perlquestion: print w/ replies, xml ) Need Help??
northwestdev has asked for the wisdom of the Perl Monks concerning the following question:

Disclaimer: Perl newbie here. I am trying to parse a Twitter response, using the Net::Twitter module. Given that the foreach loop code was taken from the Net::Twitter docs, I expected to see output, instead, I get nothing. Code:
my $nt = Net::Twitter->new( traits => [qw/API::REST/], username => $usename, password => $pwd ); my $results = $nt->show_user($twitterID); foreach my $user (@{ $results }) { my $id = $user->{id}; my $name = $user->{name}; print STDOUT $id." ".$name."<br>"; }

Comment on Parsing a Hash
Download Code
Re: Parsing a Hash
by johngg (Abbot) on Jul 21, 2009 at 21:25 UTC
    username => $usename,

    I don't know if this could be your problem but it looks like $usename might be a typo for $username. If you are not already doing so, put use strict; and use warnings; at the top of your scripts. They can help catch typos such as the above line might be.

    I hope this is helpful.

    Cheers,

    JohnGG

      Good catch, but no, it's not the misspell (it's not a misspell if it's propagated throughout the code :-)). I am using strict and warning.
Re: Parsing a Hash
by Old_Gray_Bear (Bishop) on Jul 21, 2009 at 21:32 UTC
    You probably ought to check the return from show_user(). According to the Net::Twitter documentation:

    "When Net::Twitter encounters a Twitter API error or a network error, it throws a Net::Twitter::Error object. You can catch and process these exceptions by using eval blocks and testing $@"

    The documentation has examples of how to error-proof your Twitter API calls.

    ----
    I Go Back to Sleep, Now.

    OGB

      The call is returning a valid hash. It's my syntax following the twitter API call that's problematic. When I do:
      my $nt = Net::Twitter->new( traits => [qw/API::REST/], username => $usename, password => $pwd ); my $results = $nt->show_user($twitterID); print Dumper $results;
      Here's what I get:
      $VAR1 = { 'friends_count' => 6, 'profile_background_tile' => bless( do +{\(my $o = 0)}, 'JSON::XS::Boolean' ), 'status' => { 'source' => 'Per +l Net::Twitter', 'favorited' => $VAR1->{'profile_background_tile'}, ' +truncated' => $VAR1->{'profile_background_tile'}, 'created_at' => 'Tu +e Jul 21 11:25:47 +0000 2009', 'text' => 'Checking server responses', + 'in_reply_to_user_id' => undef, 'id' => 2056026009, 'in_reply_to_sta +tus_id' => undef, 'in_reply_to_screen_name' => undef }, 'profile_imag +e_url' => 'http://s3.amazonaws.com/twitter_production/profile_images/ +000000000/January-2008__2__normal.JPG', 'profile_sidebar_fill_color' +=> 'e0ff92', 'profile_link_color' => '0000ff', 'profile_sidebar_borde +r_color' => '87bc44', 'created_at' => 'Thu Sep 11 23:45:20 +0000 2008 +', 'profile_background_color' => '9ae4e8', 'utc_offset' => -28800, 'n +otifications' => $VAR1->{'profile_background_tile'}, 'url' => 'http:/ +/www.mysite.com', 'verified' => $VAR1->{'profile_background_tile'}, ' +id' => 16249787, 'following' => $VAR1->{'profile_background_tile'}, ' +profile_background_image_url' => 'http://static.twitter.com/images/th +emes/theme1/bg.gif', 'screen_name' => 'myname', 'location' => 'WA', ' +followers_count' => 13, 'name' => 'myname', 'protected' => $VAR1->{'p +rofile_background_tile'}, 'statuses_count' => 8, 'description' => 'Te +chnology', 'profile_text_color' => '000000', 'time_zone' => 'Pacific +Time (US & Canada)', 'favourites_count' => 0 };
Re: Parsing a Hash
by davorg (Chancellor) on Jul 21, 2009 at 22:19 UTC
      I am not seeing any warnings at all. I get a blank page (except when I dump the hash, which I posted in another message on this thread, and that's the real dump of the hash, with no warnings). Here's the entire code:
      #!/usr/bin/perl use strict; use warnings; use Time::Local; use XML::DOM; use Net::Twitter; use Data::Dumper; use CGI; my $query = new CGI; print STDOUT $query->header; my $usename="myname"; my $pwd="mypwd"; my $twitterID="myname"; my $nt = Net::Twitter->new( traits => [qw/API::REST/], username => $usename, password => $pwd ); my $results = $nt->show_user($twitterID); #print Dumper $results; foreach my $user (@{ $results }) { my $id = $user->{id}; my $name = $user->{name}; print STDOUT $id." ".$name."<br>"; } print STDOUT $query->end_html;

        Well, I explained in the other thread what the problem is. The example code treats $results as an array ref but the show_user method returns a hash ref. You should get the (to my mind, really very clear) warning "Not an ARRAY reference".

        Which leaves us wondering why you're not seeing this warning when you clearly have use warnings in your code. My suspicion is that you're looking in the wrong place. All of the CGI scaffolding in your code makes me think that you're running this as a CGI program. Depending on which web server you're using and how it is configured several things can happen to warnings from CGI programs. The most common effect is that they are written to the web server error log. Did you look in there?

        You can force warnings and errors to be written to the browser by adding the following line to your code:

        use CGI::Carp qw(fatalsToBrowser warningsToBrowser);

        But remember to remove it again before putting the program into production.

        It's also worth pointing out that just because your code is a CGI program, there's no reason why you shouldn't run it from your command line in order to try it out. I never put a CGI program onto a web server without running it from the command line first.

        --

        See the Copyright notice on my home node.

        Perl training courses

Re: Parsing a Hash
by GrandFather (Cardinal) on Jul 22, 2009 at 01:02 UTC

    I see no such example code and the Data::Dumper output you show elsewhere is inconsistent with the loop. The dump shows a hash where the loop expects an array.

    Furthermore your assertion that "I expected to see output, instead, I get nothing" is inconsistent with the combination of dump and the for loop syntax. The for loop is expecting an array reference and is getting a hash reference - is should complain "Not an ARRAY reference at ...".

    The following probably gets the data you want (untried):

    my $results = $nt->show_user($twitterID); my $id = $results->{id}; my $name = $results->{name}; print STDOUT $id." ".$name."<br>";

    True laziness is hard work
      Look under Synopsis: http://search.cpan.org/~cthom/Net-Twitter-2.12/lib/Net/Twitter.pm You missed the post where I said: I get a blank page (except when I dump the hash, which I posted in another message on this thread, and that's the real dump of the hash, with no warnings).

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2014-10-26 01:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (149 votes), past polls