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-E<gt>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<"E<lt>$in">, C<"E<gt>out">.
81 returns a reference to a hash which describes internal configuration
82 of the package. Names of keys in this hash conform to standard
83 conventions with the leading C<rl_> stripped.
87 Returns a reference to a hash with keys being features present in
88 current implementation. Several optional features are used in the
89 minimal interface: C<appname> should be present if the first argument
90 to C<new> is recognized, and C<minline> should be present if
91 C<MinLine> method is not dummy. C<autohistory> should be present if
92 lines are put into history automatically (maybe subject to
93 C<MinLine>), and C<addhistory> if C<addhistory> method is not dummy.
95 If C<Features> method reports a feature C<attribs> as present, the
96 method C<Attribs> is not dummy.
100 =head1 Additional supported functions
102 Actually C<Term::ReadLine> can use some other package, that will
103 support reacher set of commands.
105 All these commands are callable via method interface and have names
106 which conform to standard conventions with the leading C<rl_> stripped.
114 The variable C<PERL_RL> governs which ReadLine clone is loaded. If the
115 value is false, a dummy interface is used. If the value is true, it
116 should be tail of the name of the package to use, such as C<Perl> or
119 If the variable is not set, the best available package is loaded.
123 package Term::ReadLine::Stub;
124 @ISA = 'Term::ReadLine::Tk';
126 $DB::emacs = $DB::emacs; # To peacify -w
128 sub ReadLine {'Term::ReadLine::Stub'}
131 my ($in,$out,$str) = @$self;
134 if not $Term::ReadLine::registered and $Term::ReadLine::toloop
135 and defined &Tk::DoOneEvent;
136 #$str = scalar <$in>;
137 $str = $self->get_line;
138 # bug in 5.000: chomping empty string creats length -1:
139 chomp $str if defined $str;
148 $console = "/dev/tty";
152 $console = "sys\$command";
155 if (defined $ENV{'OS2_SHELL'}) { # In OS/2
159 $console = "/dev/con";
163 $consoleOUT = $console;
164 $console = "&STDIN" unless defined $console;
165 if (!defined $consoleOUT) {
166 $consoleOUT = defined fileno(STDERR) ? "&STDERR" : "&STDOUT";
168 ($console,$consoleOUT);
172 die "method new called with wrong number of arguments"
173 unless @_==2 or @_==4;
174 #local (*FIN, *FOUT);
177 ($console, $consoleOUT) = findConsole;
179 open(FIN, "<$console");
180 open(FOUT,">$consoleOUT");
181 #OUT->autoflush(1); # Conflicts with debugger?
183 $| = 1; # for DB::OUT
185 bless [\*FIN, \*FOUT];
186 } else { # Filehandles supplied
187 $FIN = $_[2]; $FOUT = $_[3];
188 #OUT->autoflush(1); # Conflicts with debugger?
189 $sel = select($FOUT);
190 $| = 1; # for DB::OUT
195 sub IN { shift->[0] }
196 sub OUT { shift->[1] }
197 sub MinLine { undef }
200 my %features = (tkRunning => 1);
201 sub Features { \%features }
203 package Term::ReadLine; # So late to allow the above code be defined?
205 my $which = $ENV{PERL_RL};
207 if ($which =~ /\bgnu\b/i){
208 eval "use Term::ReadLine::Gnu;";
209 } elsif ($which =~ /\bperl\b/i) {
210 eval "use Term::ReadLine::Perl;";
212 eval "use Term::ReadLine::$which;";
214 } elsif (defined $which) { # Defined but false
217 eval "use Term::ReadLine::Gnu; 1" or eval "use Term::ReadLine::Perl; 1";
222 # To make possible switch off RL in debugger: (Not needed, work done
225 if (defined &Term::ReadLine::Gnu::readline) {
226 @ISA = qw(Term::ReadLine::Gnu Term::ReadLine::Stub);
227 } elsif (defined &Term::ReadLine::Perl::readline) {
228 @ISA = qw(Term::ReadLine::Perl Term::ReadLine::Stub);
230 @ISA = qw(Term::ReadLine::Stub);
233 package Term::ReadLine::Tk;
235 $count_handle = $count_DoOne = $count_loop = 0;
237 sub handle {$giveup = 1; $count_handle++}
240 # Tk->tkwait('variable',\$giveup); # needs Widget
241 $count_DoOne++, Tk::DoOneEvent(0) until $giveup;
248 $Term::ReadLine::registered++
249 or Tk->fileevent($self->IN,'readable',\&handle);
253 $Term::ReadLine::toloop = $_[1] if @_ > 1;
254 $Term::ReadLine::toloop;
259 $self->Tk_loop if $Term::ReadLine::toloop && defined &Tk::DoOneEvent;
260 return getc $self->IN;
265 $self->Tk_loop if $Term::ReadLine::toloop && defined &Tk::DoOneEvent;