http://www.perlmonks.org?node_id=1210528

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

Documentation: Net::Twitter

My goal is to have it return the lates status & screen name of the user searched for. I'm pretty confused by hash references, as when I use the dumper I just see this=>that=>this, but I can't figure out how to access all these things that I want.

But looking at the documentation for "lookup_users", it gives an array first, maybe since you can search multiple things, I think I figured out how to access that, but after that I'm not sure.

use warnings; use 5.010; use Net::Twitter; use Config::Tiny; use Data::Dumper qw(Dumper); use File::HomeDir; use Data::Dumper; my $nt = Net::Twitter->new( ssl => 1, traits => [qw/API::RESTv1_1/], consumer_key => '', consumer_secret => '', access_token => '', access_token_secret => '', ); my $l = $nt->lookup_users({ screen_name => 'twitter'}); my $tweet = (@{$l->{screen_name}}); print "$tweet\n";

Here I just tried to grab the screen name for a start, since the "status" is deeper. But this just gives me "Not a HASH reference"

Replies are listed 'Best First'.
Re: Twitter API - Not a hash reference
by Discipulus (Canon) on Mar 08, 2018 at 20:25 UTC
    Hello,

    Just seeing docs (never twitted inmy life..) I assume $nt->lookup_users.. returns an array so you need to iterate over element or access directly one as in:

    # totally untested! my @users = $nt->lookup_users; foreach my $usr (@users){ print $usr{screen_name}; #?? use Data:Dump dd method to see the structure you get back (better t +han Data::Dumper) dd $usr } # or directly first element returned print $users[0]{screen_name}; dd $users[0];

    I just looked here and here

    UPDATE sorry the method does not returns an array but an array ref: not big difference but different..

    # totally untested! again my $users = $nt->lookup_users; foreach my $usr (@$users){ print $usr{screen_name}; #?? use Data:Dump dd method to see the structure you get back (better t +han Data::Dumper) dd $usr } # or directly first element returned print $$users[0]->{screen_name}; dd $$users[0];

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      This works, thanks! And Data::Dump help me figure out how get to the latest tweet also :) Very helpful.
Re: Twitter API - Not a hash reference
by stevieb (Canon) on Mar 08, 2018 at 20:24 UTC

    Please edit your question with the actual data structure coming back after being rendered through Data::Dumper.

    We can then help you understand how to extract the bits you want.

        He don't have no Doctor Robert
        He don't have no Uncle Albert
        He don't even have good credit
        He can write, but he can't edit