Created
          September 27, 2011 17:40 
        
      - 
      
- 
        Save kulp/1245710 to your computer and use it in GitHub Desktop. 
    simple command-line WolframAlpha numerical evaluator
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | #!/usr/bin/env perl | |
| use warnings; | |
| use strict; | |
| use Net::WolframAlpha; | |
| my $key = $ENV{WOLFRAM_ALPHA_API_KEY} | |
| or die "set \$WOLFRAM_ALPHA_API_KEY in environment"; | |
| my $wa = Net::WolframAlpha->new(appid => $key); | |
| my $arg; | |
| if (@ARGV) { | |
| evaluate($_) for @ARGV; | |
| } else { | |
| evaluate($_) while <>; | |
| } | |
| sub evaluate | |
| { | |
| my ($arg) = @_; | |
| # Send any inputs paramters in input hash (unescaped). | |
| my $query = $wa->query( | |
| 'input' => $arg, | |
| 'scantimeout' => 3, | |
| 'format' => 'plaintext', | |
| 'podstate' => '2@More digits', | |
| ); | |
| if ($query->success) { | |
| my ($pod) = grep { $_->scanner eq "Numeric" or $_->title eq "Result" } @{ $query->pods }; | |
| (my $digits = $pod->subpods->[0]->plaintext) =~ s/\.+$//; | |
| print $digits, "\n"; | |
| } else { | |
| warn "Some error occurred\n"; | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment