Skip to content

Instantly share code, notes, and snippets.

@aubreyja
Created August 9, 2012 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aubreyja/3307022 to your computer and use it in GitHub Desktop.
Save aubreyja/3307022 to your computer and use it in GitHub Desktop.
AskSage.pm
#For command line:
use AskSage;
use strict;
use warnings;
my $python = $ARGV[0];
my $sager = AskSage -> new();
my $response = $sager -> get_response($python);
print $response."\n";
#For pg/macros/:
sub _ask_sage_init {};
sub ask_sage {
my $python = shift;
my $sager = AskSage -> new();
return $sager -> get_response($python);
}
1;
use HTTP::Tiny;
use JSON;
package AskSage;
sub new {
my $class = shift;
my $self = {
url => 'http://aleph.sagemath.org/service',
req => HTTP::Tiny->new(),
};
bless $self, $class;
return $self;
}
sub get_response {
my ($self,$python) = @_;
my $url = $self -> {url};
my $req = $self -> {req};
my $json = $self -> {json};
my $response = $req->post_form($url,{code=>$python});
my $json = $response -> {content};
my $hashref = JSON::decode_json($json);
return $hashref->{stdout};
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment