Skip to content

Instantly share code, notes, and snippets.

@kulp
Created September 27, 2011 17:40
Show Gist options
  • Save kulp/1245710 to your computer and use it in GitHub Desktop.
Save kulp/1245710 to your computer and use it in GitHub Desktop.
simple command-line WolframAlpha numerical evaluator
#!/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