Search code examples
perlapitwitterterminalcpan

Perl invalid version format error


I am trying to finish a homework for my class. After I installed a API::Twitter module using CPAN, the Net::Twitter module seems to be affected and can no longer be used.

I tried to run this code in my Mac terminal:

song_hanlun_hw8.pl

#!/usr/bin/perl

use Net::Twitter;
use JSON;
use LWP::Simple;
use XML::Bare;
use Data::Dumper;

# keys for twitter
$consumer_key    = "key";
$consumer_secret = "key";
$token           = "key-key";
$token_secret    = "key";

# keys for sentiment analysis
$apikey = "key";

# As of 13-Aug-2010, Twitter requires OAuth for authenticated requests
my $nt = Net::Twitter->new(
    traits => [qw/API::RESTv1_1/],

    # traits              => [qw/API::Search/],
    consumer_key        => $consumer_key,
    consumer_secret     => $consumer_secret,
    access_token        => $token,
    access_token_secret => $token_secret,
);

#enter a term you want to search for
$search_term = "Halloween";
$count       = 100;

my $r = $nt->search( { q => $search_term, count => $count } );

# print Dumper $r;
for my $status ( @{ $r->{statuses} } ) {
    push @tweets, $status->{text};
}

$nextMaxId = $r->{search_metadata}->{next_results};
$nextMaxId =~ s/\?max_id=//g;
$nextMaxId =~ s/&q=Halloween&count=100&include_entities=1//g;

# print $nextMaxId;

my $r2 = $nt->search( { q => $search_term, count => $count, max_id => $nextMaxId } );
for my $status ( @{ $r2->{statuses} } ) {
    push @tweets, $status->{text};
}

foreach $tweet ( @tweets ) {

    # $tweet = $tweets[1];
    $return    = get "http://gateway-a.watsonplatform.net/calls/text/TextGetTextSentiment?apikey=$apikey&text=$tweet&outputMode=xml";
    $bare      = new XML::Bare( text => $return );
    $root      = $bare->parse();
    $sentiment = $root->{results}->{docSentiment}->{score}->{value};
    push @sentiments, $sentiment;

    # print "$sentiment";
}

$m100to75 = 0;
$m75to50  = 0;
$m50to25  = 0;
$m25to0   = 0;
$p0to25   = 0;
$p25to50  = 0;
$p50to75  = 0;
$p75to100 = 0;

foreach $sent ( @sentiments ) {

    # smaller than zero. four ranges.
    if ( $sent >= -1 && $sent < -0.75 ) {
        $m100to75++;
    }

    if ( $sent >= -0.75 && $sent < -0.5 ) {
        $m75to50++;
    }

    if ( $sent >= -0.5 && $sent < -0.25 ) {
        $m50to25++;
    }

    if ( $sent >= -0.25 && $sent < 0 ) {
        $m25to0++;
    }

    #bigger than zero. four ranges.
    if ( $sent >= 0 && $sent < 0.25 ) {
        $p0to25++;
    }

    if ( $sent >= 0.25 && $sent < 0.5 ) {
        $p25to50++;
    }

    if ( $sent >= 0.5 && $sent < 0.75 ) {
        $p50to75++;
    }

    if ( $sent >= 0.75 && $sent < 1 ) {
        $p75to100++;
    }

}

# print "$m100to75\n$m75to50\n$m50to25\n$m25to0\n$p0to25\n$p25to50\n$p50to75\n$p75to100\n";

print "tweets sentiment score summary histogram:\n";

print "-1.00 to -0.75: ";
for ( $i = 0 ; $i < $m100to75 ; $i++ ) {
    print "*";
}
print "\n";

print "-0.75 to -0.50: ";
for ( $i = 0 ; $i < $m75to50 ; $i++ ) {
    print "*";
}
print "\n";

print "-0.50 to -0.25: ";
for ( $i = 0 ; $i < $m50to25 ; $i++ ) {
    print "*";
}
print "\n";

print "-0.25 to -0.00: ";
for ( $i = 0 ; $i < $m25to0 ; $i++ ) {
    print "*";
}
print "\n";

print "+0.00 to +0.25: ";
for ( $i = 0 ; $i < $p0to25 ; $i++ ) {
    print "*";
}
print "\n";

print "+0.25 to +0.50: ";
for ( $i = 0 ; $i < $p25to50 ; $i++ ) {
    print "*";
}
print "\n";

print "+0.50 to +0.75: ";
for ( $i = 0 ; $i < $p50to75 ; $i++ ) {
    print "*";
}
print "\n";

print "+0.75 to +1.00: ";
for ( $i = 0 ; $i < $p75to100 ; $i++ ) {
    print "*";
}
print "\n";

But the terminal returns

Invalid version format (version required) at /Library/Perl/5.18/Module/Runtime.pm line 386.
BEGIN failed--compilation aborted at /Library/Perl/5.18/Net/Twitter.pm line 3.
Compilation failed in require at song_hanlun_hw8.pl line 3.
BEGIN failed--compilation aborted at song_hanlun_hw8.pl line 3.

Could someone tell me how to fix this?


Solution

  • After some Google search I highly believe downgrading the Module::Runtime would solve this issue. But I could't find how.

    Eventually I solved this issue by delete the Runtime module file in perl folder and reinstalled the Net::Twitter module, which also reinstalled the Runtime that Net::Twitter depends on.