source: lab/trunk/twitter/show_status.pl @ 62

Last change on this file since 62 was 62, checked in by mitty, 14 years ago
  • add caution
  • Property svn:executable set to *
File size: 1.6 KB
Line 
1#! /usr/bin/perl -w
2
3use strict;
4use warnings;
5use 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
12use Net::Twitter::Lite;
13use Data::Dumper;
14
15my $bot = Net::Twitter::Lite->new;
16
17eval {
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};
36if ($@) {
37    evalrescue($@);
38}
39print "truncated output done\n";
40
41
42sub 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}
Note: See TracBrowser for help on using the repository browser.