3 Term::ReadLine - Perl interface to various C<readline> packages.
4 If no real package is found, substitutes stubs instead of basic functions.
9 my $term = Term::ReadLine->new('Simple Perl calc');
10 my $prompt = "Enter your arithmetic expression: ";
11 my $OUT = $term->OUT || \*STDOUT;
12 while ( defined ($_ = $term->readline($prompt)) ) {
15 print $OUT $res, "\n" unless $@;
16 $term->addhistory($_) if /\S/;
21 This package is just a front end to some other packages. It's a stub to
22 set up a common interface to the various ReadLine implementations found on
23 CPAN (under the C<Term::ReadLine::*> namespace).
25 =head1 Minimal set of supported functions
27 All the supported functions should be called as methods, i.e., either as
29 $term = Term::ReadLine->new('name');
33 $term->addhistory('row');
35 where $term is a return value of Term::ReadLine-E<gt>new().
41 returns the actual package that executes the commands. Among possible
42 values are C<Term::ReadLine::Gnu>, C<Term::ReadLine::Perl>,
43 C<Term::ReadLine::Stub>.
47 returns the handle for subsequent calls to following
48 functions. Argument is the name of the application. Optionally can be
49 followed by two arguments for C<IN> and C<OUT> filehandles. These
50 arguments should be globs.
54 gets an input line, I<possibly> with actual C<readline>
55 support. Trailing newline is removed. Returns C<undef> on C<EOF>.
59 adds the line to the history of input, from where it can be used if
60 the actual C<readline> is present.
64 return the filehandles for input and output or C<undef> if C<readline>
65 input and output cannot be used for Perl.
69 If argument is specified, it is an advice on minimal size of line to
70 be included into history. C<undef> means do not include anything into
71 history. Returns the old value.
75 returns an array with two strings that give most appropriate names for
76 files for input and output using conventions C<"E<lt>$in">, C<"E<gt>out">.
80 returns a reference to a hash which describes internal configuration
81 of the package. Names of keys in this hash conform to standard
82 conventions with the leading C<rl_> stripped.
86 Returns a reference to a hash with keys being features present in
87 current implementation. Several optional features are used in the
88 minimal interface: C<appname> should be present if the first argument
89 to C<new> is recognized, and C<minline> should be present if
90 C<MinLine> method is not dummy. C<autohistory> should be present if
91 lines are put into history automatically (maybe subject to
92 C<MinLine>), and C<addhistory> if C<addhistory> method is not dummy.
94 If C<Features> method reports a feature C<attribs> as present, the
95 method C<Attribs> is not dummy.
99 =head1 Additional supported functions
101 Actually C<Term::ReadLine> can use some other package, that will
102 support a richer set of commands.
104 All these commands are callable via method interface and have names
105 which conform to standard conventions with the leading C<rl_> stripped.
107 The stub package included with the perl distribution allows some
114 makes Tk event loop run when waiting for user input (i.e., during
119 makes the command line stand out by using termcap data. The argument
120 to C<ornaments> should be 0, 1, or a string of a form
121 C<"aa,bb,cc,dd">. Four components of this string should be names of
122 I<terminal capacities>, first two will be issued to make the prompt
123 standout, last two to make the input line standout.
127 takes two arguments which are input filehandle and output filehandle.
128 Switches to use these filehandles.
132 One can check whether the currently loaded ReadLine package supports
133 these methods by checking for corresponding C<Features>.
141 The environment variable C<PERL_RL> governs which ReadLine clone is
142 loaded. If the value is false, a dummy interface is used. If the value
143 is true, it should be tail of the name of the package to use, such as
146 As a special case, if the value of this variable is space-separated,
147 the tail might be used to disable the ornaments by setting the tail to
148 be C<o=0> or C<ornaments=0>. The head should be as described above, say
150 If the variable is not set, or if the head of space-separated list is
151 empty, the best available package is loaded.
153 export "PERL_RL=Perl o=0" # Use Perl ReadLine without ornaments
154 export "PERL_RL= o=0" # Use best available ReadLine without ornaments
156 (Note that processing of C<PERL_RL> for ornaments is in the discretion of the
157 particular used C<Term::ReadLine::*> package).
161 It seems that using Term::ReadLine from Emacs minibuffer doesn't work
162 quite right and one will get an error message like
164 Cannot open /dev/tty for read at ...
166 One possible workaround for this is to explicitly open /dev/tty like this
168 open (FH, "/dev/tty" )
169 or eval 'sub Term::ReadLine::findConsole { ("&STDIN", "&STDERR") }';
173 or you can try using the 4-argument form of Term::ReadLine->new().
179 package Term::ReadLine::Stub;
180 our @ISA = qw'Term::ReadLine::Tk Term::ReadLine::TermCap';
182 $DB::emacs = $DB::emacs; # To peacify -w
184 *rl_term_set = \@Term::ReadLine::TermCap::rl_term_set;
186 sub PERL_UNICODE_STDIN () { 0x0001 }
188 sub ReadLine {'Term::ReadLine::Stub'}
191 my ($in,$out,$str) = @$self;
193 print $out $rl_term_set[0], $prompt, $rl_term_set[1], $rl_term_set[2];
195 if not $Term::ReadLine::registered and $Term::ReadLine::toloop
196 and defined &Tk::DoOneEvent;
197 #$str = scalar <$in>;
198 $str = $self->get_line;
199 $str =~ s/^\s*\Q$prompt\E// if ($^O eq 'MacOS');
201 if (${^UNICODE} & PERL_UNICODE_STDIN || defined ${^ENCODING}) &&
203 print $out $rl_term_set[3];
204 # bug in 5.000: chomping empty string creats length -1:
205 chomp $str if defined $str;
214 if ($^O eq 'MacOS') {
215 $console = "Dev:Console";
216 } elsif (-e "/dev/tty") {
217 $console = "/dev/tty";
218 } elsif (-e "con" or $^O eq 'MSWin32') {
220 $consoleOUT = 'CONOUT$';
222 $console = "sys\$command";
225 if (($^O eq 'amigaos') || ($^O eq 'beos') || ($^O eq 'epoc')) {
228 elsif ($^O eq 'os2') {
232 $console = "/dev/con";
236 $consoleOUT = $console unless defined $consoleOUT;
237 $console = "&STDIN" unless defined $console;
238 if (!defined $consoleOUT) {
239 $consoleOUT = defined fileno(STDERR) && $^O ne 'MSWin32' ? "&STDERR" : "&STDOUT";
241 ($console,$consoleOUT);
245 die "method new called with wrong number of arguments"
246 unless @_==2 or @_==4;
247 #local (*FIN, *FOUT);
248 my ($FIN, $FOUT, $ret);
250 my($console, $consoleOUT) = $_[0]->findConsole;
253 # the Windows CONIN$ needs GENERIC_WRITE mode to allow
254 # a SetConsoleMode() if we end up using Term::ReadKey
255 open FIN, ( $^O eq 'MSWin32' && $console eq 'CONIN$' ) ? "+<$console" :
257 open FOUT,">$consoleOUT";
259 #OUT->autoflush(1); # Conflicts with debugger?
260 my $sel = select(FOUT);
261 $| = 1; # for DB::OUT
263 $ret = bless [\*FIN, \*FOUT];
264 } else { # Filehandles supplied
265 $FIN = $_[2]; $FOUT = $_[3];
266 #OUT->autoflush(1); # Conflicts with debugger?
267 my $sel = select($FOUT);
268 $| = 1; # for DB::OUT
270 $ret = bless [$FIN, $FOUT];
272 if ($ret->Features->{ornaments}
273 and not ($ENV{PERL_RL} and $ENV{PERL_RL} =~ /\bo\w*=0/)) {
274 local $Term::ReadLine::termcap_nowarn = 1;
281 my ($self, $in, $out) = @_;
284 my $sel = select($out);
285 $| = 1; # for DB::OUT
289 sub IN { shift->[0] }
290 sub OUT { shift->[1] }
291 sub MinLine { undef }
294 my %features = (tkRunning => 1, ornaments => 1, 'newTTY' => 1);
295 sub Features { \%features }
304 package Term::ReadLine; # So late to allow the above code be defined?
306 our $VERSION = '1.03';
308 my ($which) = exists $ENV{PERL_RL} ? split /\s+/, $ENV{PERL_RL} : undef;
310 if ($which =~ /\bgnu\b/i){
311 eval "use Term::ReadLine::Gnu;";
312 } elsif ($which =~ /\bperl\b/i) {
313 eval "use Term::ReadLine::Perl;";
315 eval "use Term::ReadLine::$which;";
317 } elsif (defined $which and $which ne '') { # Defined but false
320 eval "use Term::ReadLine::Gnu; 1" or eval "use Term::ReadLine::Perl; 1";
325 # To make possible switch off RL in debugger: (Not needed, work done
328 if (defined &Term::ReadLine::Gnu::readline) {
329 @ISA = qw(Term::ReadLine::Gnu Term::ReadLine::Stub);
330 } elsif (defined &Term::ReadLine::Perl::readline) {
331 @ISA = qw(Term::ReadLine::Perl Term::ReadLine::Stub);
332 } elsif (defined $which && defined &{"Term::ReadLine::$which\::readline"}) {
333 @ISA = "Term::ReadLine::$which";
335 @ISA = qw(Term::ReadLine::Stub);
338 package Term::ReadLine::TermCap;
340 # Prompt-start, prompt-end, command-line-start, command-line-end
341 # -- zero-width beautifies to emit around prompt and the command line.
342 our @rl_term_set = ("","","","");
344 our $rl_term_set = ',,,';
348 return if defined $terminal;
351 $terminal = Tgetent Term::Cap ({OSPEED => 9600}); # Avoid warning.
356 return $rl_term_set unless @_;
357 $rl_term_set = shift;
358 $rl_term_set ||= ',,,';
359 $rl_term_set = 'us,ue,md,me' if $rl_term_set eq '1';
360 my @ts = split /,/, $rl_term_set, 4;
361 eval { LoadTermCap };
362 unless (defined $terminal) {
363 warn("Cannot find termcap: $@\n") unless $Term::ReadLine::termcap_nowarn;
364 $rl_term_set = ',,,';
367 @rl_term_set = map {$_ ? $terminal->Tputs($_,1) || '' : ''} @ts;
372 package Term::ReadLine::Tk;
374 our($count_handle, $count_DoOne, $count_loop);
375 $count_handle = $count_DoOne = $count_loop = 0;
378 sub handle {$giveup = 1; $count_handle++}
381 # Tk->tkwait('variable',\$giveup); # needs Widget
382 $count_DoOne++, Tk::DoOneEvent(0) until $giveup;
389 $Term::ReadLine::registered++
390 or Tk->fileevent($self->IN,'readable',\&handle);
394 $Term::ReadLine::toloop = $_[1] if @_ > 1;
395 $Term::ReadLine::toloop;
400 $self->Tk_loop if $Term::ReadLine::toloop && defined &Tk::DoOneEvent;
401 return getc $self->IN;
406 $self->Tk_loop if $Term::ReadLine::toloop && defined &Tk::DoOneEvent;