2008-05-15 03:20:41 -04:00
|
|
|
#!/usr/bin/perl
|
|
|
|
#
|
|
|
|
# by Erik Osheim
|
|
|
|
#
|
|
|
|
# licensed under the GNU GPL version 2
|
|
|
|
|
2008-05-16 09:24:17 -04:00
|
|
|
# try not to pollute the namespace
|
|
|
|
use Data::Dumper qw(Dumper);
|
|
|
|
use File::Basename qw();
|
|
|
|
use Getopt::Long qw();
|
|
|
|
use Perl6::Slurp qw(slurp);
|
|
|
|
use Scalar::Util qw();
|
2008-05-18 10:39:08 -04:00
|
|
|
|
|
|
|
# NOTE: some Term::ReadLine implementations don't support features that we sort
|
|
|
|
# of need. Term::ReadLine::Gnu is recommended.
|
2008-05-16 09:24:17 -04:00
|
|
|
use Term::ReadLine qw();
|
2008-05-15 03:20:41 -04:00
|
|
|
|
2008-05-16 16:27:03 -04:00
|
|
|
# globals
|
2008-05-15 03:20:41 -04:00
|
|
|
Getopt::Long::Configure('bundling');
|
|
|
|
$| = 1;
|
|
|
|
$Data::Dumper::Terse = 1;
|
|
|
|
$Data::Dumper::Indent = 1;
|
2008-05-16 16:27:03 -04:00
|
|
|
my $PIPE;
|
2008-05-15 03:20:41 -04:00
|
|
|
|
2008-05-16 16:27:03 -04:00
|
|
|
# usage function
|
2008-05-15 03:20:41 -04:00
|
|
|
sub usage {
|
|
|
|
my($status) = @_;
|
2008-05-16 09:24:17 -04:00
|
|
|
my $prog = File::Basename::basename($0);
|
2008-05-15 03:20:41 -04:00
|
|
|
print <<EOT;
|
|
|
|
usage: $prog [options]
|
|
|
|
-h,--help show this message
|
|
|
|
-e,--eval STR eval STR before interaction
|
2008-05-19 10:51:10 -04:00
|
|
|
-p,--pipe run in pipe mode
|
|
|
|
-q,--quiet hide undef eval output
|
2008-05-15 03:20:41 -04:00
|
|
|
-r,--run PATH run PATH before interaction
|
2008-05-19 10:51:10 -04:00
|
|
|
-v,--verbose output all eval results (default)
|
2008-05-15 03:20:41 -04:00
|
|
|
|
|
|
|
perl needs a good interpreter.
|
|
|
|
EOT
|
|
|
|
exit($status);
|
|
|
|
}
|
|
|
|
|
2008-05-16 16:27:03 -04:00
|
|
|
# handy function for inspecting the symbol table
|
2008-05-15 03:20:41 -04:00
|
|
|
my $pkgregex = qr/^(?:[a-zA-Z_][a-zA-Z0-9_]*::)+$/;
|
|
|
|
my $nameregex = qr/^[a-zA-Z_][a-zA-Z0-9_]*$/;
|
2008-05-18 10:39:08 -04:00
|
|
|
|
|
|
|
# get all the objects of a particular sigil-type rooted in a package
|
|
|
|
sub sigilread {
|
2008-05-15 03:20:41 -04:00
|
|
|
my ($pkg, $sigil, $base, $proc) = @_;
|
2008-05-18 10:39:08 -04:00
|
|
|
my $base = $pkg eq 'main::' ? '' : $pkg;
|
2008-05-15 03:20:41 -04:00
|
|
|
$proc = {$pkg => 1} unless $proc;
|
|
|
|
my @results;
|
|
|
|
foreach my $item (eval("keys(\%${pkg})")) {
|
|
|
|
my $item2 = $base . $item;
|
|
|
|
next if $proc->{$item2};
|
|
|
|
if($item =~ m/$nameregex/) {
|
2008-05-18 10:39:08 -04:00
|
|
|
if($sigil eq '$') {
|
|
|
|
if(eval("defined(\$::${item2})")) {
|
|
|
|
push(@results, $item2);
|
|
|
|
} elsif(eval("defined(\@::${item2})")) {
|
|
|
|
push(@results, $item2 . "[");
|
|
|
|
} elsif(eval("defined(\%::${item2})")) {
|
|
|
|
push(@results, $item2 . "{");
|
|
|
|
}
|
|
|
|
} elsif(eval("defined(${sigil}::${item2})")) {
|
|
|
|
push(@results, $item2);
|
|
|
|
}
|
2008-05-15 03:20:41 -04:00
|
|
|
} elsif($item =~ m/$pkgregex/) {
|
|
|
|
$proc->{$item2} = 1;
|
2008-05-18 10:39:08 -04:00
|
|
|
push(@results, sigilread($item2, $sigil, $item2, $proc));
|
|
|
|
my $pkgname = substr($item2, 0, -2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return @results;
|
|
|
|
}
|
|
|
|
|
|
|
|
# find single-step completions for a particular package/name
|
|
|
|
sub sigilfind {
|
|
|
|
my ($pkg, $name, $proc) = @_;
|
|
|
|
my $base = $pkg eq 'main::' ? '' : $pkg;
|
|
|
|
$proc = {$pkg => 1} unless $proc;
|
|
|
|
my @results;
|
|
|
|
foreach my $item (eval("keys(\%${pkg})")) {
|
|
|
|
my $item2 = $base . $item;
|
|
|
|
next if $proc->{$item2};
|
|
|
|
if($item2 =~ m/^$name/) {
|
|
|
|
if($item =~ m/$nameregex/ && eval("defined(&::${item2})")) {
|
|
|
|
push(@results, $item2);
|
|
|
|
} elsif($item =~ m/$pkgregex/) {
|
|
|
|
$proc->{$item2} = 1;
|
|
|
|
my $pkgname = substr($item2, 0, -2);
|
|
|
|
if($name eq $pkgname) {
|
|
|
|
push(@results, "$pkgname->");
|
|
|
|
push(@results, sigilfind($item2, $name, $proc));
|
|
|
|
} else {
|
|
|
|
push(@results, $pkgname);
|
|
|
|
}
|
|
|
|
}
|
2008-05-15 03:20:41 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return @results;
|
|
|
|
}
|
|
|
|
|
2008-05-18 10:39:08 -04:00
|
|
|
# find all functions accessible via a package's @ISA array. this could probably
|
|
|
|
# be implemented in a more efficient manner.
|
|
|
|
sub isafind {
|
|
|
|
my ($pkg, $proc, $names) = @_;
|
|
|
|
return if $proc && $proc->{$pkg};
|
|
|
|
$proc = {$pkg => 1} unless $proc;
|
|
|
|
$names = {} unless $names;
|
|
|
|
|
|
|
|
my @pkgs = eval("\@${pkg}::ISA");
|
|
|
|
foreach my $pkg2 (@pkgs) {
|
|
|
|
isafind($pkg2, $proc, $names);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach my $item (eval("keys(\%${pkg}::)")) {
|
|
|
|
if($item =~ m/$nameregex/) {
|
|
|
|
$names->{$item} = 1 if eval("defined(&::${pkg}::${item})");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return keys(%$names);
|
|
|
|
}
|
|
|
|
|
2008-05-16 16:27:03 -04:00
|
|
|
# handy function for printing nice representations of data
|
|
|
|
sub repr {
|
2008-05-15 03:20:41 -04:00
|
|
|
my ($item) = @_;
|
|
|
|
if(!defined($item)) {
|
|
|
|
return 'undef';
|
|
|
|
} elsif(Scalar::Util::blessed($item)) {
|
|
|
|
return "$item";
|
|
|
|
} elsif(ref($item) eq 'CODE') {
|
|
|
|
return "$item";
|
|
|
|
} else {
|
|
|
|
my $s = Dumper($item);
|
|
|
|
$s =~ s/\n+$//;
|
|
|
|
return $s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-18 10:39:08 -04:00
|
|
|
# find the object referenced by a name
|
|
|
|
sub resolve_ref {
|
|
|
|
my ($name) = @_;
|
|
|
|
my ($obj, $reftype, $package);
|
|
|
|
$obj = eval("$name");
|
|
|
|
my $reftype = $obj ? Scalar::Util::reftype($obj) : '';
|
|
|
|
my $package = $obj ? ref($obj) : '';
|
|
|
|
return ($obj, $reftype, $package);
|
|
|
|
}
|
|
|
|
|
2008-05-16 16:27:03 -04:00
|
|
|
# use sigilfind to get completions for particular word(s)
|
2008-05-15 03:20:41 -04:00
|
|
|
sub complete {
|
2008-05-18 10:39:08 -04:00
|
|
|
my ($word, $line, $x1, $x2) = @_;
|
2008-05-15 03:20:41 -04:00
|
|
|
$word =~ m/^([&\$%@\*]?)(.*)$/;
|
2008-05-18 10:39:08 -04:00
|
|
|
my $sigil = $1;
|
2008-05-15 03:20:41 -04:00
|
|
|
my $name = $2;
|
2008-05-18 10:39:08 -04:00
|
|
|
my $pre = substr($line, 0, $x1);
|
2008-05-19 01:53:31 -04:00
|
|
|
|
|
|
|
if(length($line) == $x1 && $pre =~ m/^[ \t]*$/) {
|
|
|
|
# hack to handle the case where we really do want a tab
|
|
|
|
return ("\t");
|
|
|
|
}
|
|
|
|
|
2008-05-18 10:39:08 -04:00
|
|
|
if($pre =~ m/(\$(?:[a-zA-Z_][a-zA-Z0-9_]*::)*[a-zA-Z_][a-zA-Z0-9_]*)->$/) {
|
|
|
|
# ref dereference
|
|
|
|
my($obj, $reftype, $package) = resolve_ref($1);
|
|
|
|
if($reftype eq $package) {
|
|
|
|
if($reftype eq 'CODE') {
|
|
|
|
return ("$word\(");
|
|
|
|
} elsif($reftype eq 'HASH') {
|
|
|
|
return ("$word\{");
|
|
|
|
} elsif($reftype eq 'ARRAY') {
|
|
|
|
return ("$word\[");
|
|
|
|
} else {
|
|
|
|
return ();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
my @names = isafind($package);
|
|
|
|
return grep { $_ =~ m/^$word/ } @names;
|
|
|
|
}
|
|
|
|
} elsif($pre =~ m/((?:[a-zA-Z_][a-zA-Z0-9_]*::)*[a-zA-Z_][a-zA-Z0-9_]*)->$/) {
|
|
|
|
# package dereference
|
|
|
|
my @names = isafind($1);
|
|
|
|
return grep { $_ =~ m/^$word/ } @names;
|
|
|
|
} elsif($pre =~ m/\$((?:[a-zA-Z_][a-zA-Z0-9_]*::)*[a-zA-Z_][a-zA-Z0-9_]*){$/) {
|
|
|
|
# hash keys
|
|
|
|
my $obj = eval("\\\%$1");
|
|
|
|
return $obj ? grep { $_ =~ m/^$word/ } keys(%$obj) : ();
|
|
|
|
} elsif($pre =~ m/(\$(?:[a-zA-Z_][a-zA-Z0-9_]*::)*[a-zA-Z_][a-zA-Z0-9_]*)->{$/) {
|
|
|
|
# hashref keys
|
|
|
|
my $obj = eval("$1");
|
|
|
|
return $obj ? grep { $_ =~ m/^$word/ } keys(%$obj) : ();
|
|
|
|
} else {
|
|
|
|
# literals and ambiguous cases
|
|
|
|
$name =~ m/^((?:[a-zA-Z_][a-zA-Z0-9_]*::)*)/;
|
|
|
|
my $pkgname = $1 ? $1 : 'main::';
|
|
|
|
my @candidates;
|
|
|
|
if($sigil) {
|
|
|
|
# if we have a sigil, then we can just jump to the actual object
|
|
|
|
# completions.
|
|
|
|
@candidates = map {
|
|
|
|
"${sigil}${_}"
|
|
|
|
} sigilread($pkgname, $sigil);
|
|
|
|
} else {
|
|
|
|
# if we don't have a sigil, the user might wanna call a function,
|
|
|
|
# or complete a package to make an object-oriented call. so complete
|
|
|
|
# step-by-step to avoid annoying the user.
|
|
|
|
@candidates = map {
|
|
|
|
"${sigil}${_}"
|
|
|
|
} grep {
|
|
|
|
$_ =~ m/^$name/
|
|
|
|
} sigilfind($pkgname, $name);
|
|
|
|
}
|
|
|
|
return sort(@candidates);
|
|
|
|
}
|
2008-05-15 03:20:41 -04:00
|
|
|
}
|
|
|
|
|
2008-05-16 16:27:03 -04:00
|
|
|
# display completions to the user
|
|
|
|
sub draw_completions {
|
|
|
|
my (@items) = @_;
|
|
|
|
my($prefix, $delim) = $pipe ? ("COMPLETIONS", "|") : ("", "\n");
|
|
|
|
print $prefix . join($delim, @items) . "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
# some help text
|
2008-05-15 03:20:41 -04:00
|
|
|
my $HELP = <<EOT;
|
|
|
|
Greetings!
|
|
|
|
|
|
|
|
I'm program is designed to help you write Perl. Like the "perl" command,
|
|
|
|
I can parse and execute Perl code. Unlike it, I can execute perl code
|
|
|
|
interactively, allowing you to call functions, save their results, inspect
|
2008-05-16 16:27:03 -04:00
|
|
|
the results, and repeat. I also support tab-completion!
|
2008-05-15 03:20:41 -04:00
|
|
|
|
|
|
|
I contain a lot of tricky functionality.
|
|
|
|
|
|
|
|
One side-note: I can't be run under the "strict" pragma; thus, you don't
|
|
|
|
need to use my() or our() to declare variables. The reaon for this has to
|
2008-05-16 16:27:03 -04:00
|
|
|
do with the way I evaluate blocks. Sorry.
|
2008-05-15 03:20:41 -04:00
|
|
|
EOT
|
|
|
|
|
2008-05-16 16:27:03 -04:00
|
|
|
# the big one!
|
2008-05-15 03:20:41 -04:00
|
|
|
sub main {
|
2008-05-18 10:39:08 -04:00
|
|
|
# ugh stupid fucking Term::ReadLine's automatic escapes
|
2008-05-20 08:55:13 -04:00
|
|
|
my ($prompt1, $prompt2) = (">>>", '..>');
|
|
|
|
#my ($prompt1, $prompt2) = ("\001\033[24m\002>>>", "\001\033[24m\002..>");
|
2008-05-18 10:39:08 -04:00
|
|
|
|
2008-05-16 16:27:03 -04:00
|
|
|
# process the arguments provided; save original @ARGV for use later
|
2008-05-19 10:51:10 -04:00
|
|
|
my $quiet;
|
2008-05-16 16:27:03 -04:00
|
|
|
my @preload;
|
|
|
|
my @oldargv = @ARGV;
|
|
|
|
Getopt::Long::GetOptions(
|
|
|
|
'help|h' => sub { usage(0); },
|
|
|
|
'eval|e=s' => sub { push(@preload, ['eval', $_[1]]); },
|
|
|
|
'pipe|p' => sub { $pipe = 'line' },
|
|
|
|
'run|r=s' => sub { push(@preload, ['run', $_[1]]); },
|
|
|
|
'use|u=s' => sub { push(@preload, ['use', $_[1]]); },
|
2008-05-19 10:51:10 -04:00
|
|
|
'quiet|q' => sub { $quiet = 1; },
|
|
|
|
'verbose|v' => sub { $quiet = undef; },
|
2008-05-16 16:27:03 -04:00
|
|
|
) || usage(1);
|
|
|
|
|
|
|
|
# let's display a nice banner to the user
|
2008-05-18 10:39:08 -04:00
|
|
|
#my ($prompt, $input, $term) = (">>>", "", undef);
|
|
|
|
my ($prompt, $input, $term) = ($prompt1, "", undef);
|
2008-05-16 16:27:03 -04:00
|
|
|
unless($pipe) {
|
|
|
|
print "Welcome to Erik's Perl Interpreter\n";
|
|
|
|
print "(Type \":help\", \":exit\", or something else)\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
# for every file or perl string we were given, eval them in order; we should
|
|
|
|
# die if any of them fail to load.
|
2008-05-15 03:20:41 -04:00
|
|
|
foreach my $pair (@preload) {
|
|
|
|
if($pair->[0] eq 'eval') {
|
|
|
|
eval $pair->[1];
|
|
|
|
die "failed to eval '$pair->[1]': $@" if $@;
|
|
|
|
} elsif($pair->[0] eq 'run') {
|
|
|
|
die "no path named $pair->[1] found" unless -e $pair->[1];
|
|
|
|
my $data = slurp($pair->[1]);
|
|
|
|
eval $data;
|
|
|
|
die "failed to run $pair->[1]: $@" if $@;
|
|
|
|
} elsif($pair->[0] eq 'use') {
|
|
|
|
eval "use $pair->[1]";
|
|
|
|
die "failed to use $pair->[1]: $@" if $@;
|
|
|
|
}
|
|
|
|
}
|
2008-05-16 16:27:03 -04:00
|
|
|
|
|
|
|
# set up readline if necessary
|
2008-05-15 03:20:41 -04:00
|
|
|
unless($pipe) {
|
|
|
|
$term = Term::ReadLine->new('IPERL');
|
|
|
|
my $attribs = $term->Attribs();
|
|
|
|
$attribs->{special_prefixes} = '$@%*&';
|
2008-05-18 10:39:08 -04:00
|
|
|
$attribs->{completion_function} = \&complete;
|
|
|
|
$attribs->{completion_append_character} = '';
|
2008-05-15 03:20:41 -04:00
|
|
|
}
|
|
|
|
|
2008-05-16 16:27:03 -04:00
|
|
|
# the mighty read-exec-print loop!
|
2008-05-15 03:20:41 -04:00
|
|
|
while(1) {
|
2008-05-16 16:27:03 -04:00
|
|
|
# display the prompt and read some input
|
2008-05-15 03:20:41 -04:00
|
|
|
my $line;
|
|
|
|
if($pipe) {
|
2008-05-16 16:27:03 -04:00
|
|
|
print "PROMPT:$prompt\n";
|
2008-05-15 03:20:41 -04:00
|
|
|
$line = <STDIN>;
|
|
|
|
} else {
|
|
|
|
$line = $term->readline("$prompt ");
|
|
|
|
}
|
|
|
|
last unless defined($line);
|
|
|
|
chomp($line);
|
|
|
|
|
2008-05-16 16:27:03 -04:00
|
|
|
# if we're in pipe-mode then we expect input in a special form
|
2008-05-15 03:20:41 -04:00
|
|
|
if($pipe) {
|
|
|
|
if($line =~ m/ENTER:(.*)$/) {
|
|
|
|
$line = $1;
|
2008-05-16 16:27:03 -04:00
|
|
|
} elsif($line =~ m/COMPLETE:(.*)$/) {
|
|
|
|
draw_completions(complete($1));
|
|
|
|
next;
|
2008-05-15 03:20:41 -04:00
|
|
|
} else {
|
2008-05-16 16:27:03 -04:00
|
|
|
print "malformed pipe input line\n";
|
2008-05-15 03:20:41 -04:00
|
|
|
next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-16 16:27:03 -04:00
|
|
|
# process the line of input
|
2008-05-15 03:20:41 -04:00
|
|
|
if($line eq '') {
|
2008-05-16 16:27:03 -04:00
|
|
|
} elsif($line eq ':exit' || $line eq ':quit') {
|
2008-05-15 03:20:41 -04:00
|
|
|
last;
|
2008-05-16 16:27:03 -04:00
|
|
|
} elsif($line eq ':help') {
|
2008-05-18 10:39:08 -04:00
|
|
|
($input, $prompt) = ("", $prompt1);
|
2008-05-16 09:24:17 -04:00
|
|
|
print $HELP;
|
2008-05-16 16:27:03 -04:00
|
|
|
} elsif($line eq ':reload') {
|
2008-05-18 10:39:08 -04:00
|
|
|
($input, $prompt) = ("", $prompt1);
|
2008-05-16 09:24:17 -04:00
|
|
|
print "reloading...\n";
|
2008-05-16 16:27:03 -04:00
|
|
|
exec($0, @oldargv);
|
|
|
|
} elsif($line eq ':sh') {
|
2008-05-18 10:39:08 -04:00
|
|
|
($input, $prompt) = ("", $prompt1);
|
2008-05-15 03:20:41 -04:00
|
|
|
system("bash") unless $pipe;
|
2008-05-16 16:27:03 -04:00
|
|
|
} elsif($line =~ m/^:sh (.+)$/) {
|
2008-05-18 10:39:08 -04:00
|
|
|
($input, $prompt) = ("", $prompt1);
|
2008-05-15 03:20:41 -04:00
|
|
|
system($1) unless $pipe;
|
|
|
|
} elsif($line eq ':code') {
|
2008-05-18 10:39:08 -04:00
|
|
|
draw_completions(sort(sigilread('main::', '&')));
|
2008-05-15 03:20:41 -04:00
|
|
|
} elsif($line eq ':scalar') {
|
2008-05-18 10:39:08 -04:00
|
|
|
draw_completions(sort(sigilread('main::', '$')));
|
2008-05-15 03:20:41 -04:00
|
|
|
} elsif($line eq ':hash') {
|
2008-05-18 10:39:08 -04:00
|
|
|
draw_completions(sort(sigilread('main::', '%')));
|
2008-05-15 03:20:41 -04:00
|
|
|
} elsif($line eq ':array') {
|
2008-05-18 10:39:08 -04:00
|
|
|
draw_completions(sort(sigilread('main::', '@')));
|
2008-05-15 03:20:41 -04:00
|
|
|
} elsif($line eq ':glob') {
|
2008-05-18 10:39:08 -04:00
|
|
|
draw_completions(sort(sigilread('main::', '*')));
|
|
|
|
} elsif($line =~ m/^:isa (.+)$/) {
|
|
|
|
draw_completions(sort(isafind($1)));
|
2008-05-16 16:27:03 -04:00
|
|
|
} elsif($line =~ m/^[\t ]/ || $line =~ m/\{ *$/ || m/\\ *$/) {
|
|
|
|
# we're dealing with an incomplete statement, so defer execution
|
2008-05-15 03:20:41 -04:00
|
|
|
$line =~ s/\\ *$//;
|
|
|
|
$input .= $line;
|
2008-05-18 10:39:08 -04:00
|
|
|
$prompt = $prompt2;
|
2008-05-15 03:20:41 -04:00
|
|
|
} else {
|
2008-05-16 16:27:03 -04:00
|
|
|
# we're dealing with a complete statement, so execute and display
|
2008-05-15 03:20:41 -04:00
|
|
|
$input .= $line;
|
2008-05-16 16:27:03 -04:00
|
|
|
my @results = map { repr($_) } eval($input);
|
2008-05-15 03:20:41 -04:00
|
|
|
if($@) {
|
2008-05-16 09:24:17 -04:00
|
|
|
print $@;
|
2008-05-16 16:27:03 -04:00
|
|
|
} elsif(scalar(@results) == 0) {
|
|
|
|
} elsif(scalar(@results) == 1) {
|
2008-05-19 10:51:10 -04:00
|
|
|
if(!$quiet || $results[0] ne 'undef') {
|
|
|
|
print $results[0] . "\n";
|
|
|
|
}
|
2008-05-15 03:20:41 -04:00
|
|
|
} else {
|
2008-05-16 09:24:17 -04:00
|
|
|
print join(", ", @results) . "\n";
|
2008-05-15 03:20:41 -04:00
|
|
|
}
|
2008-05-18 10:39:08 -04:00
|
|
|
($input, $prompt) = ("", $prompt1);
|
2008-05-15 03:20:41 -04:00
|
|
|
}
|
|
|
|
}
|
2008-05-16 09:24:17 -04:00
|
|
|
print "Bye.\n";
|
2008-05-15 03:20:41 -04:00
|
|
|
}
|
|
|
|
main();
|