Term-ReadLine-Gnu
view release on metacpan or search on metacpan
#!/usr/bin/env perl
#
#
# Copyright (c) 1996 Hiroo Hayashi. All Rights Reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
=head1 NAME
perlsh - one-line perl evaluator with line editing function and
variable name completion function
=head1 SYNOPSIS
perlsh
=head1 DESCRIPTION
This program reads input a line, and evaluates it by perl interpreter,
and prints the result. If the result is a list value then each value
of the list is printed line by line. This program can be used as a
very strong calculator which has whole perl functions.
This is a sample program Term::ReadLine::Gnu module. When you input a
line, the line editing function of GNU Readline Library is available.
Perl symbol name completion function is also available.
=cut
package PerlSh;
use strict;
use warnings;
use Term::ReadLine;
use POSIX; # for sigaction below
use vars qw($PS1 $PS2 $HISTFILE $HISTSIZE $INPUTRC $STRICT
$HOSTNAME $LOGNAME $CWP);
#$PS1 = '$ ';
$PS1='\w[\!]$ ';
$PS2 = '> ';
$HISTFILE = ($ENV{HOME} || ((getpwuid($<))[7])) . "/.perlsh_history";
$HISTSIZE = 256;
$INPUTRC = ($ENV{HOME} || ((getpwuid($<))[7])) . "/.perlshrc";
$STRICT = 0;
$HOSTNAME = $ENV{HOSTNAME};
$LOGNAME = $ENV{LOGNAME};
$CWP = 'main'; # current working package
package main;
if (-f $PerlSh::INPUTRC) {
do $PerlSh::INPUTRC;
}
package PerlSh;
use vars qw($term $attribs); # to access as `$PerlSh::term' from prompt
$term = new Term::ReadLine 'PerlSh';
$attribs = $term->Attribs;
$term->bind_key(ord "^", 'history-expand-line', 'emacs-meta');
$term->bind_key(ord "\cv", 'display-readline-version', 'emacs-ctlx');
$term->bind_key(ord "\cc", 'abort'); # not works yet FIXME!!!
if (defined &main::afterinit) {
package main;
&afterinit;
package PerlSh;
}
&toplevel; # never returns
########################################################################
sub toplevel {
# disable implicit add_history() call
$term->MinLine(undef);
$term->stifle_history($HISTSIZE);
if (-f $HISTFILE) {
$term->ReadHistory($HISTFILE)
or warn "perlsh: cannot read history file: $!\n";
}
$attribs->{attempted_completion_function} = \&attempt_perl_completion;
$attribs->{special_prefixes} = '$@%&';
$attribs->{completion_display_matches_hook}
= \&perl_symbol_display_match_list;
# See http://perldoc.perl.org/perlipc.html#Deferred-Signals-%28Safe-Signals%29
# was '$SIG{INT} = sub { ...'
sigaction SIGINT, new POSIX::SigAction sub {
$term->modifying;
$term->delete_text;
$attribs->{point} = $attribs->{end} = 0;
$term->redisplay;
} or die "Error setting SIGINT handler: $!\n";
my ($strict, $command, @result);
$strict = $STRICT ? '' : 'no strict;';
while (defined($command = &reader)) {
@result = eval ("$strict package $CWP; $command");
use strict;
if ($@) { print "Error: $@\n"; next; }
printer (@result);
'@' => 'ARRAY', '$#' => 'ARRAY',
'%' => 'HASH',
'&' => 'CODE'); # '
# from perl5.004_02 perlfunc
@keyword = qw(
chomp chop chr crypt hex index lc lcfirst
length oct ord pack q qq
reverse rindex sprintf substr tr uc ucfirst
y
m pos quotemeta s split study qr
abs atan2 cos exp hex int log oct rand sin
sqrt srand
pop push shift splice unshift
grep join map qw reverse sort unpack
delete each exists keys values
binmode close closedir dbmclose dbmopen die
eof fileno flock format getc print printf
read readdir rewinddir seek seekdir select
syscall sysread sysseek syswrite tell telldir
truncate warn write
pack read syscall sysread syswrite unpack vec
chdir chmod chown chroot fcntl glob ioctl
link lstat mkdir open opendir readlink rename
rmdir stat symlink umask unlink utime
caller continue die do dump eval exit goto
last next redo return sub wantarray
caller import local my package use
defined dump eval formline local my reset
scalar undef wantarray
alarm exec fork getpgrp getppid getpriority
kill pipe qx setpgrp setpriority sleep
system times wait waitpid
do import no package require use
bless dbmclose dbmopen package ref tie tied
untie use
accept bind connect getpeername getsockname
getsockopt listen recv send setsockopt shutdown
socket socketpair
msgctl msgget msgrcv msgsnd semctl semget
semop shmctl shmget shmread shmwrite
endgrent endhostent endnetent endpwent getgrent
getgrgid getgrnam getlogin getpwent getpwnam
getpwuid setgrent setpwent
endprotoent endservent gethostbyaddr
gethostbyname gethostent getnetbyaddr
getnetbyname getnetent getprotobyname
getprotobynumber getprotoent getservbyname
getservbyport getservent sethostent setnetent
setprotoent setservent
gmtime localtime time times
abs bless chomp chr exists formline glob
import lc lcfirst map my no prototype qx qw
readline readpipe ref sub sysopen tie tied
uc ucfirst untie use
dbmclose dbmopen
);
}
}
__END__
=pod
Before invoking, this program reads F<~/.perlshrc> and evaluates the
content of the file.
When this program is terminated, the content of the history buffer is
saved in a file F<~/.perlsh_history>, and it is read at next
invoking.
=head1 VARIABLES
You can customize the behavior of C<perlsh> by setting following
variables in F<~/.perlshrc>;
=over 4
=item C<$PerlSh::PS1>
The primary prompt string. The following backslash-escaped special
characters can be used.
\h: host name
\u: user name
\w: package name
\!: history number
The default value is `C<\w[\!]$ >'.
=item C<$PerlSh::PS2>
The secondary prompt string. The default value is `C<E<gt> >'.
=item C<$PerlSh::HISTFILE>
The name of the file to which the command history is saved. The
default value is C<~/.perlsh_history>.
=item C<$PerlSh::HISTSIZE>
( run in 2.969 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )