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