#!/usr/bin/perl
use warnings;
use RL;
use Data::Dumper;

while (1) {
    my $line = RL::readline("");
    if (!defined $line) {
        print "\n";
        last;
    }
    if ($line =~ /^(q|quit|e|exit)$/) {
        last;
    }
    if (!length($line)) {
        next;
    }
    RL::add_history($line);
    my $output = eval $line;
    if ($@) {
        my $error = $@;
        $error =~ s/at \(eval \d+\) line \d+, /at /;
        print STDERR "$error";
    }
    elsif (ref $output) {
        print dumper($output);
    }
    elsif (defined $output) {
        print "$output\n";
    }
}

sub dumper {
    $Data::Dumper::Sortkeys = 1;
    $Data::Dumper::Indent = 1;
    $Data::Dumper::Useqq = 1;
    $Data::Dumper::Quotekeys = 0;
    my $output = "";
    for my $item (@_) {
        my $dd = Data::Dumper->new([$item]);
        $dd->{"xpad"} = "    ";
        my $output2 = $dd->Dump();
        $output2 =~ s/^\$VAR1 = |;$//g;
        $output .= $output2;
    }
    return $output;
}

__END__

=head1 NAME

pl - A Perl repl for running Perl commands interactively on the command line

=head1 SYNOPSIS

    pl

=head1 OPTIONS

none

=head1 DESCRIPTION

This is a command for running Perl statements interactively. You
type a command and then the program prints the results. If the
result is a reference (array, hash, blessed hash, etc.) it will
print it out using Data::Dumper. It uses the RL module (readline)
so you can press up and down to navigate your previously typed
entries, as well as other line editing features.

=cut

