Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6832449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:51:36+00:00 2026-05-26T22:51:36+00:00

I’m using the AnyEvent::Twitter::Stream module to grab tweets. Ultimately I’m trying to print the

  • 0

I’m using the AnyEvent::Twitter::Stream module to grab tweets. Ultimately I’m trying to print the tweets to a file but I’m unable (I think) to get the tweet as a JSON object. My code is as follows:

#!/Applications/XAMPP/xamppfiles/bin/perl

use AnyEvent::Twitter::Stream;
    my $done = AnyEvent->condvar;

BEGIN {
    use Exporter;
    our @ISA = qw(Exporter);
    our @EXPORT = qw{
       &init
    };
}

sub print_tweet {
  my $tweet = shift;
  print $tweet;   
}


  # receive updates from @following_ids
  my $listener = AnyEvent::Twitter::Stream->new(
      username => XXXXXX
      password => XXXXXX
      method   => 'sample',  # "firehose" for everything, "sample" for sample timeline
      decode_json => 1,
      on_tweet => sub {
          my $tweet = shift;
          print_tweet($tweet);
      },
      on_keepalive => sub {
          warn "ping\n";
      },
      on_delete => sub {
          my ($tweet_id, $user_id) = @_; # callback executed when twitter send a delete notification
      },
      timeout => 45,
  );

$done->recv;

Yet when I print out the tweet in the print_tweet subroutine all I get is:

HASH(0x8f0ad0)HASH(0x8f0640)HASH(0x875990)HASH(0x8f0ab0)HASH(0x8e0d80)HASH(0x8f06e0)HASH(0x8f08f0)HASH(0x93ef30)HASH(0x876190)HASH(0x93ee60)HASH(0x8f0610)HASH(0x8f0b00)HASH(0x8e13e0)HASH(0x93ee20)HASH(0x8f0a20)HASH(0x8e1970)HASH(0x8f0900)

I’ve even tried to print out the tweet assuming it is a hash as follows:

sub print_tweet {
  my ($jsonref, $tweet) = @_;
  my $tweet = shift;
  print %tweet;
}

Yet that produced nothing. It appears that AnyEvent::Twitter::Stream is returning $tweet as an object based on their sample code of:

  on_tweet => sub {
      my $tweet = shift;
      warn "$tweet->{user}{screen_name}: $tweet->{text}\n";
  },

And I know I can print out individual objects, but can I get teh raw JSON object? I must be missing something or my ‘noob’ness is greater than I thought…

UPDATE

I was able to ALMOST get it by changing print_tweet to the following:

sub print_tweet {
  my $tweet = shift;
  my $json_output = to_json($tweet);
  print $json_output;
}

It prints out MOST of the JSON object but complains about wide characters, which I believe is an issue with the output being utf8 format? I’m unsure how to solve this issue though….

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:51:37+00:00Added an answer on May 26, 2026 at 10:51 pm

    Figured it out. Need to use the JSON module and encode. When encoding you MUST use the {utf8 => 1} option to account for the utf8 characters you get form Twitter. Final code is here:

    #!/Applications/XAMPP/xamppfiles/bin/perl
    
    use JSON;
    use utf8;
    use AnyEvent::Twitter::Stream;
        my $done = AnyEvent->condvar;
    
    BEGIN {
        use Exporter;
        our @ISA = qw(Exporter);
        our @EXPORT = qw{
           &init
        };
    }
    
    sub print_tweet {  
      my $tweet = shift;
      my $json_output = to_json($tweet, {utf8 => 1});
      print $json_output;
      print "\n";
    }
    
    
      # receive updates from @following_ids
      my $listener = AnyEvent::Twitter::Stream->new(
          username => XXXXXXXX
          password => XXXXXXXX
          method   => 'sample',  # "firehose" for everything, "sample" for sample timeline
          on_tweet => sub {
              my $tweet = shift;
              print_tweet($tweet);
          },
          on_keepalive => sub {
              warn "ping\n";
          },
          on_delete => sub {
              my ($tweet_id, $user_id) = @_; # callback executed when twitter send a delete notification
          },
          timeout => 45,
      );
    
    $done->recv;
    

    Thanks to the help you guys gave, the DataDumper at least let me verify the format, it just didn’t produce the final result.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am trying to render a haml file in a javascript response like so:
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.