* remove needless hash check
[lab.git] / show_status.pl
1 #! /usr/bin/perl -w
2
3 use strict;
4 use warnings;
5 use utf8;
6
7 ## IMPORTANT ##
8 # When Net::Twitter::Lite encounters a Twitter API error or a network error, 
9 # it throws a Net::Twitter::Lite::Error object. 
10 # You can catch and process these exceptions by using eval blocks and testing $@
11 ## from http://search.cpan.org/perldoc?Net::Twitter::Lite#ERROR_HANDLING
12 use Net::Twitter::Lite;
13 use Data::Dumper;
14
15 my $bot = Net::Twitter::Lite->new;
16
17 eval {
18     foreach my $id (@ARGV) {
19         my $res = $bot->show_status($id);
20         foreach my $line (split /\n/, Dumper $res) {
21             if ($line =~ /undef/) { next; }
22             unless ($line =~ / => {/
23                 ||  $line =~ / = /
24                 ||  $line =~ /status/
25                 ||  $line =~ /'text'/
26                 ||  $line =~ /created/
27                 ||  $line =~ /'id'/
28                 ||  $line =~ /name/
29                 ||  $line =~ / },/
30                 ||  $line =~ / };/
31             ) { next; }
32             print $line, "\n";
33         }
34     }
35 };
36 if ($@) {
37     evalrescue($@);
38 }
39 print "truncated output done\n";
40
41
42 sub evalrescue {
43     # output error message at eval error
44     
45     use Scalar::Util qw(blessed);
46     
47     if (blessed $@ && $@->isa('Net::Twitter::Lite::Error')) {
48         warn $@->error;
49         if ($@->twitter_error) {
50             my %twitter_error = %{$@->twitter_error};
51             map {
52                 $twitter_error{"$_ => "} = $twitter_error{$_} . "\n";
53                 delete $twitter_error{$_}
54             } keys %twitter_error;
55             warn join("", %twitter_error);
56         }
57     }
58     else {
59         warn $@;
60     }
61 }