3 Term::ReadLine - Perl interface to various C<readline> packages. If
4 no real package is found, substitutes stubs instead of basic functions.
9 $term = new Term::ReadLine 'Simple Perl calc';
10 $prompt = "Enter your arithmetic expression: ";
11 $OUT = $term->OUT || STDOUT;
12 while ( defined ($_ = $term->readline($prompt)) ) {
13 $res = eval($_), "\n";
15 print $OUT $res, "\n" unless $@;
16 $term->addhistory($_) if /\S/;
21 This package is just a front end to some other packages. At the moment
22 this description is written, the only such package is Term-ReadLine,
23 available on CPAN near you. The real target of this stub package is to
24 set up a common interface to whatever Readline emerges with time.
26 =head1 Minimal set of supported functions
28 All the supported functions should be called as methods, i.e., either as
30 $term = new Term::ReadLine 'name';
34 $term->addhistory('row');
36 where $term is a return value of Term::ReadLine->Init.
42 returns the actual package that executes the commands. Among possible
43 values are C<Term::ReadLine::Gnu>, C<Term::ReadLine::Perl>,
44 C<Term::ReadLine::Stub Exporter>.
48 returns the handle for subsequent calls to following
49 functions. Argument is the name of the application. Optionally can be
50 followed by two arguments for C<IN> and C<OUT> filehandles. These
51 arguments should be globs.
55 gets an input line, I<possibly> with actual C<readline>
56 support. Trailing newline is removed. Returns C<undef> on C<EOF>.
60 adds the line to the history of input, from where it can be used if
61 the actual C<readline> is present.
65 return the filehandles for input and output or C<undef> if C<readline>
66 input and output cannot be used for Perl.
70 If argument is specified, it is an advice on minimal size of line to
71 be included into history. C<undef> means do not include anything into
72 history. Returns the old value.
76 returns an array with two strings that give most appropriate names for
77 files for input and output using conventions C<"<$in">, C<"E<gt>out">.
81 Returns a reference to a hash with keys being features present in
82 current implementation. Several optional features are used in the
83 minimal interface: C<appname> should be present if the first argument
84 to C<new> is recognized, and C<minline> should be present if
85 C<MinLine> method is not dummy. C<autohistory> should be present if
86 lines are put into history automatically (maybe subject to
87 C<MinLine>), and C<addhistory> if C<addhistory> method is not dummy.
91 Actually C<Term::ReadLine> can use some other package, that will
92 support reacher set of commands.
100 package Term::ReadLine::Stub;
102 $DB::emacs = $DB::emacs; # To peacify -w
104 sub ReadLine {'Term::ReadLine::Stub'}
106 my ($in,$out,$str) = @{shift()};
109 # bug in 5.000: chomping empty string creats length -1:
110 chomp $str if defined $str;
119 $console = "/dev/tty";
123 $console = "sys\$command";
126 if (defined $ENV{'OS2_SHELL'}) { # In OS/2
130 $console = "/dev/con";
134 $consoleOUT = $console;
135 $console = "&STDIN" unless defined $console;
136 if (!defined $consoleOUT) {
137 $consoleOUT = defined fileno(STDERR) ? "&STDERR" : "&STDOUT";
139 ($console,$consoleOUT);
143 die "method new called with wrong number of arguments"
144 unless @_==2 or @_==4;
145 #local (*FIN, *FOUT);
148 ($console, $consoleOUT) = findConsole;
150 open(FIN, "<$console");
151 open(FOUT,">$consoleOUT");
152 #OUT->autoflush(1); # Conflicts with debugger?
154 $| = 1; # for DB::OUT
156 bless [\*FIN, \*FOUT];
157 } else { # Filehandles supplied
158 $FIN = $_[2]; $FOUT = $_[3];
159 #OUT->autoflush(1); # Conflicts with debugger?
160 $sel = select($FOUT);
161 $| = 1; # for DB::OUT
166 sub IN { shift->[0] }
167 sub OUT { shift->[1] }
168 sub MinLine { undef }
171 package Term::ReadLine; # So late to allow the above code be defined?
172 eval "use Term::ReadLine::Gnu;" or eval "use Term::ReadLine::Perl;";
176 # To make possible switch off RL in debugger: (Not needed, work done
179 if (defined &Term::ReadLine::Gnu::readline) {
180 @ISA = qw(Term::ReadLine::Gnu Term::ReadLine::Stub);
181 } elsif (defined &Term::ReadLine::Perl::readline) {
182 @ISA = qw(Term::ReadLine::Perl Term::ReadLine::Stub);
184 @ISA = qw(Term::ReadLine::Stub);