1 Replies - 1600 Views - Last Post: 05 December 2008 - 08:11 PM

#1 mocker   User is offline

  • D.I.C Regular
  • member icon

Reputation: 51
  • View blog
  • Posts: 466
  • Joined: 14-October 07

Command line Google Calculator

Posted 21 November 2008 - 09:29 AM

Description: required modules: URI::Escape; LWP::UserAgent Run from the command line and enter your equation at the prompt. Type "quit" to exitreads equations from stdin and queries google's calculator to find answer note: parses html response, so is dependent on googles markup staying the same
#!/usr/bin/perl

#testing math functions

use URI::Escape;
use LWP::UserAgent;

print "Command line Google Calculator (enter your equation and press enter): n";
while(<>){
$raw_input = $_;
if($raw_input =~ /quit/ ){
die "nuser exited";
}
else {
$raw_input = $_ ;
my $sum = calc_google($raw_input);
print $sum . "n New Equation: n";
}

}

sub calc_google {

unless ( $_ ) {
    $result = "usage: !math expression  - example: !math 75 kg in stones";
    return $result;
}
$qstring = $_ ;
chomp($qstring);
my $url = 'http://www.google.com/search?num=1&q=' .
        uri_escape($qstring);

my $ua = LWP::UserAgent->new( agent => 'Mozilla/5.0' );
my $response = $ua->get( $url );

$response->is_success or
    return "Error!: ", $response->status_line;

my $content = $response->content;

$content =~ /(.*?)/ig ;
$result1 = $1;
if ( $result1 ) {
    $result1 =~ s/<.+?>//g;
    return "Answer! $result1";
} else {
    return "No result for:  $qstring";
}



Is This A Good Question/Topic? 0
  • +

Replies To: Command line Google Calculator

#2 KevinADC   User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 401
  • Joined: 23-January 07

Re: Command line Google Calculator

Posted 05 December 2008 - 08:11 PM

Your code does not pass a basic syntax check. It appears to be missing the closing curly bracket for sub calc_google. Once that is fixed the script appears to work well.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1