-
-
Save yappo/77265 to your computer and use it in GitHub Desktop.
This file contains 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
use strict; | |
use warnings; | |
use URI::Escape 'uri_escape'; | |
use LWP::UserAgent; | |
use JSON 'decode_json'; | |
use Term::ReadLine; | |
my $ua = LWP::UserAgent->new(agent => "Dan the shell"); | |
&main;exit; | |
sub main { | |
my $term = Term::ReadLine->new("Dan the shell"); | |
while (defined($_ = $term->readline("dan> "))) { | |
do_dan($_); | |
$term->addhistory($_) if /\S/; | |
} | |
} | |
sub do_dan { | |
my $src = shift; | |
$_ =~ s/\n//; | |
if ($src =~ /^ls\s+(.+)$/) { | |
$src = sprintf q{opendir my $d, '%s' or die $!;print join "\n", map { "%s/$_" } readdir($d);}, $1, $1; | |
} elsif ($src =~ /^cat\s+(.+)$/) { | |
$src = sprintf q{open my $f, '%s' or die $!;print do { local $/; <$f>;}}, $1; | |
} elsif ($src eq 'ENV') { | |
$src = 'while (my($k, $v) = each %ENV) { print "$k => $v\n" }'; | |
} | |
my $url = "http://api.dan.co.jp/perleval.cgi?c=callback&s=" . uri_escape($src); | |
my $res = $ua->get($url); | |
if ($res->is_success) { | |
my $json = $res->content; | |
$json =~ s/callback\((.+)\);\n/$1/; | |
my $dat = eval{ decode_json $json }; | |
if ($dat->{error}) { | |
print $dat->{error}, "\n"; | |
} elsif (defined $dat->{result}) { | |
print $dat->{result}, "\n"; | |
} else { | |
print "invalid json: $json\n"; | |
} | |
} else { | |
print $res->status_line, "\n"; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment