#!/usr/bin/perl -w $0 =~ s/.*\///; use 5.010; # -*- perl -*- # Simple read-eval-print loop, intended as a calculator. # put into the public domain by Juergen Nickelsen my @val = (); # list of values our $rcfile = "$ENV{HOME}/.calcrc.pl" ; # Evaluated on startup $| = 1; if (-r $rcfile) { eval { do $rcfile }; warn("$0: error evaluating $rcfile: $@") if $@; } sub val { return $val[shift() || $#val]; } sub h {"Enter Perl expressions to evaluate, e.g. \"9979.93630573248 * 3.14\"."} sub help { return h(); } $SIG{INT} = sub { print("\n" . @val . "; "); }; if (@ARGV) { print((eval("@ARGV") || '') . "\n"); } else { while (print(" "), $_ = <>) { if (/\S/) { my @value = eval(); my $err = $@; if (@value) { push(@val, @value); map {$_ = "" unless defined($_)} @value; print(@val . "; "); if (@value > 1) { print("(", join(", ", @value), ")\n"); } else { print("@value\n"); } } else { print($err || "\n"); } } } print("\n"); } #EOF