use Term::ReadLine;
my $term = new Term::ReadLine 'Simple Perl calc';
my $prompt = "Enter your arithmetic expression: ";
my $OUT = $term->OUT || \*STDOUT;
while ( defined ($_ = $term->readline($prompt)) ) {
my $res = eval($_);
warn $@ if $@;
print $OUT $res, "\n" unless $@;
$term->addhistory($_) if /\S/;
}
This package is just a front end to some other packages. It's a stub to
set up a common interface to the various ReadLine implementations found on CPAN (under the Term::ReadLine::* namespace).
returns the handle for subsequent calls to following
functions. Argument is the name of the application. Optionally can be
followed by two arguments for IN and OUT filehandles. These
arguments should be globs.
If argument is specified, it is an advice on minimal size of line to
be included into history. undef means do not include anything into
history. Returns the old value.
returns a reference to a hash which describes internal configuration
of the package. Names of keys in this hash conform to standard
conventions with the leading rl_ stripped.
Returns a reference to a hash with keys being features present in
current implementation. Several optional features are used in the
minimal interface: appname should be present if the first argument
to new is recognized, and minline should be present ifMinLine method is not dummy. autohistory should be present if lines are put into history automatically (maybe subject to
MinLine), and addhistoryifaddhistory method is not dummy.
If Features method reports a feature attribs as present, the
method Attribs is not dummy.
makes the command line stand out by using termcap data. The argument
to ornaments should be 0, 1, or a string of a form
"aa,bb,cc,dd". Four components of this string should be names of
terminal capacities, first two will be issued to make the prompt
standout, last two to make the input line standout.
The environment variable PERL_RL governs which ReadLine clone is
loaded. If the value is false, a dummy interface is used. If the value
is true, it should be tail of the name of the package to use, such as
Perl or Gnu.
As a special case, if the value of this variable is space-separated,
the tail might be used to disable the ornaments by setting the tail to
be o=0 or ornaments=0. The head should be as described above, say
If the variable is not set, or if the head of space-separated list is
empty, the best available package is loaded.
export "PERL_RL=Perl o=0" # Use Perl ReadLine without ornaments
export "PERL_RL= o=0" # Use best available ReadLine without ornaments
(Note that processing of PERL_RL for ornaments is in the discretion of the
particular used Term::ReadLine::* package).