#!/usr/bin/perl
# Author: Alex Efros <powerman-asdf@yandex.ru>, 2008,2013,2016
# License: Public Domain
#
# pcalc: Perl-based console calculator.
#
# Support readline, command history, and aliases:
#   x $var                  Data::Dumper::Dumper($var)
#   db $db,$user,$pass      $dbh = DBI->connect($db,$user,$pass)

use utf8;
use feature ':5.10';
use Data::Dumper;
use Benchmark qw( cmpthese );
use DBI;
use AnyEvent;
use AnyEvent::DBI::MySQL;
use DBIx::SecureCGI;
use Term::ReadLine;
our $VERSION = 1.12;
$Data::Dumper::Sortkeys = 1;

our $dbh;
sub db { $dbh = AnyEvent::DBI::MySQL->connect("dbi:mysql:$_[0]", $_[1], $_[2]) or warn DBI->errstr }
sub x { goto &Dumper }

$pcalc::name = 'pcalc';
$pcalc::t = Term::ReadLine->new($pcalc::name);
select $pcalc::t->OUT;
$pcalc::h = glob '~/.'.$pcalc::name.'_history';
$pcalc::t->ReadHistory($pcalc::h);
while (defined($_ = $pcalc::t->readline("$pcalc::name> "))) {
    next unless $_;
    print eval, "\n";
    warn $@ if $@;
}
my %seen;
$pcalc::t->SetHistory(reverse grep {!$seen{$_}++} reverse $pcalc::t->GetHistory());
$pcalc::t->WriteHistory($pcalc::h);
print "\n";

