Acme::Math::Google - Let Google do the math


Acme-Math-Google documentation Contained in the Acme-Math-Google distribution.

Index


Code Index:

NAME

Top

Acme::Math::Google - Let Google do the math

SYNOPSIS

Top

  use Acme::Math::Google;
  my $amg = Acme::Math::Google->new;
  print $amg->calc('e**(i*pi)');    # -1
  print $amg->calc('e**(i*pi)', 1); # 'e ** (i * pi) = -1'
                                    # WWW::Google::Calculator compat

DESCRIPTION

Top

Need I say more than above?

EXPORT

None.

Acme::Math::Google vs. WWW::Google::Calculator

Darn, another wheel reinvented. This module does essentially the same as WWW::Google::Calculator but much simpler. All you need is URI and LWP::UserAgent whereas WWW::Google::Calculator demands WWW::Mechanize, HTML::TokeParser and Class::Accessor::Fast.

SEE ALSO

Top

WWW::Google::Calculator

http://www.google.com/intl/en/help/features.html#calculator

AUTHOR

Top

Dan Kogai, <dankogai@dan.co.jp>

COPYRIGHT AND LICENSE

Top


Acme-Math-Google documentation Contained in the Acme-Math-Google distribution.

package Acme::Math::Google;
use 5.008001;
use strict;
use warnings;

our $VERSION = sprintf "%d.%02d", q$Revision: 0.2 $ =~ /(\d+)/g;
use URI;
use LWP::UserAgent;

sub new{
    my $class = shift;
    my $self  = shift || {};
    $self->{base_uri} ||= 'http://www.google.com/search';
    unless ($self->{ua}){
	my $ua = LWP::UserAgent->new;
	$ua->agent( __PACKAGE__ . '/' . $VERSION );
	$self->{ua} = $ua;
    }
    return bless $self, $class;
}

sub calc{
    my $self  = shift;
    my $query = shift;
    my $as_equation = shift;
    my $uri = URI->new($self->{base_uri});
    $uri->query_form( q => $query);
    my $res = $self->{ua}->get($uri);
    return unless $res->code == 200;
    my $ans = $res->content;
    $ans =~ s{.*/images/calc_img.gif}{}xmso;
    $ans =~ s{.*?<b>}{}xmso;
    $ans =~ s{</b>.*}{}xmso;
    return $ans if $as_equation;
    $ans =~ s{.*=\s+}{}xmso;
    return $ans;
}

1;
__END__
# Below is stub documentation for your module. You'd better edit it!