#!/usr/bin/perl -w

my $PROMPT1 = ">>> ";
my $PROMPT2 = "... ";
my $AUTOPRINT = 1;
my @STACK = ();

use Term::ReadLine;
$ENV{'PERL_RL'} =" o=0";  # Use best available ReadLine without ornaments
my $term = new Term::ReadLine 'Perlthon';
my $prompt = $PROMPT1;

my $OUT = $term->OUT || \*STDOUT;

printf $OUT "Perlthon running Perl %vd\n", $^V;
print  $OUT 'Type "help;" for help, "exit;" or press CTRL+D to exit', "\n";

while ( defined ($_ = $term->readline($prompt)) ) {
    if (/;\s*$/) {
	my @RESULT = eval join "\n", (@STACK, $_);
	print $OUT @RESULT if $AUTOPRINT; 
	print $OUT "\n";
	$prompt=$PROMPT1;
	@STACK=();
	$term->addhistory(join " ", (@STACK, $_)) if /[^;\s]/;
    }else{
	push @STACK, $_;
	$prompt=$PROMPT2;
    }
}

sub quit() {
    print $OUT qq(Use "exit;" or CTRL+D to exit.\n);
    return "";
}
sub help() {
    print $OUT <<"EOF";
Perlthon, the Interactive Perl Interpeter v0.1
by Jason Clark  <jason\@jclark.org>
This is Free Software.

Enter commands for perl to evaluate, a la interactive Python.
Lines without a ; are continued on next input.
By default, the result of each evauation is printed.  To disable, 
use this: "\$AUTOPRINT=0;"

Use "exit;" or CTRL+D to exit.

Prompts are controlled by \$PROMPT1 and \$PROMPT2, which you can change.
EOF
    return ""; #prevent extra output when $AUTOPRINT
}
