File Coverage

blib/lib/WWW/Google/Calculator.pm
Criterion Covered Total %
statement 18 43 41.8
branch 0 4 0.0
condition 0 12 0.0
subroutine 6 9 66.6
pod 3 3 100.0
total 27 71 38.0


line stmt bran cond sub pod time code
1             package WWW::Google::Calculator;
2 2     2   746 use strict;
  2         4  
  2         75  
3 2     2   10 use warnings;
  2         4  
  2         65  
4 2     2   11 use base qw/Class::Accessor::Fast/;
  2         3  
  2         1130  
5              
6 2     2   6830 use WWW::Mechanize;
  2         338062  
  2         96  
7 2     2   27 use HTML::TokeParser;
  2         4  
  2         46  
8 2     2   13 use URI;
  2         5  
  2         893  
9              
10             our $VERSION = '0.07';
11              
12             __PACKAGE__->mk_accessors(qw/mech error/);
13              
14             =head1 NAME
15              
16             WWW::Google::Calculator - Perl interface for Google calculator
17              
18             =head1 SYNOPSIS
19              
20             use WWW::Google::Calculator;
21            
22             my $calc = WWW::Google::Calculator->new;
23            
24             print $calc->calc('1+1'); # => 1 + 1 = 2
25             print $calc->calc('300kbps in KB/s'); # => 300 kbps = 37.5 kilobytes / second
26              
27             =head1 DESCRIPTION
28              
29             This module provide simple interface for Google calculator.
30              
31             =head1 SEE ALSO
32              
33             http://www.google.com/help/calculator.html
34              
35             =head1 METHODS
36              
37             =head2 new
38              
39             create new instance
40              
41             =cut
42              
43             sub new {
44 0     0 1   my $self = shift->SUPER::new(@_);
45              
46             $self->mech(
47 0           do {
48 0           my $mech = WWW::Mechanize->new;
49 0           $mech->agent_alias('Linux Mozilla');
50              
51 0           $mech;
52             }
53             );
54              
55 0           $self;
56             }
57              
58             =head2 calc( $query )
59              
60             calculate $query using by Google and return result.
61              
62             return undef when something error occurred. (and use $self->error to get last error message)
63              
64             =cut
65              
66             sub calc {
67 0     0 1   my ( $self, $query ) = @_;
68              
69 0           my $url = URI->new('http://www.google.com/search');
70 0           $url->query_form( q => $query );
71              
72 0           $self->mech->get($url);
73 0 0         if ($self->mech->success) {
74 0           return $self->parse_html( $self->mech->content );
75             }
76             else {
77 0           $self->error( 'Page fetching failed: ' . $self->mech->res->status_line );
78 0           return;
79             }
80             }
81              
82             =head2 parse_html
83              
84             =cut
85              
86             sub parse_html {
87 0     0 1   my ( $self, $html ) = @_;
88              
89 0           $html =~ s!(.*?)!^$1!g;
90 0           $html =~ s!×!*!g;
91              
92 0           my $res;
93 0           my $p = HTML::TokeParser->new( \$html );
94 0           while ( my $token = $p->get_token ) {
95             next
96 0 0 0       unless ( $token->[0] || '' ) eq 'S'
      0        
      0        
      0        
      0        
97             && ( $token->[1] || '' ) eq 'img'
98             && ( $token->[2]->{src} || '' ) eq '/images/icons/onebox/calculator-40.gif';
99              
100 0           $p->get_tag('h2');
101 0           $res = $p->get_trimmed_text('/h2');
102 0           return $res; # stop searching here
103             }
104              
105 0           $res;
106             }
107              
108             =head2 error
109              
110             return last error
111              
112             =head1 AUTHOR
113              
114             Daisuke Murase
115              
116             =head1 COPYRIGHT
117              
118             This program is free software; you can redistribute
119             it and/or modify it under the same terms as Perl itself.
120              
121             The full text of the license can be found in the
122             LICENSE file included with this module.
123              
124             =cut
125              
126             1;