Exporting UNIVERSAL::can
[p5sagit/p5-mst-13.2.git] / lib / Term / ReadLine.pm
CommitLineData
cb31d310 1=head1 NAME
2
c07a80fd 3Term::ReadLine - Perl interface to various C<readline> packages. If
cb31d310 4no real package is found, substitutes stubs instead of basic functions.
5
6=head1 SYNOPSIS
7
8 use Term::ReadLine;
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";
14 warn $@ if $@;
15 print $OUT $res, "\n" unless $@;
16 $term->addhistory($_) if /\S/;
17 }
18
c07a80fd 19=head1 DESCRIPTION
20
21This package is just a front end to some other packages. At the moment
22this description is written, the only such package is Term-ReadLine,
23available on CPAN near you. The real target of this stub package is to
24set up a common interface to whatever Readline emerges with time.
25
cb31d310 26=head1 Minimal set of supported functions
27
28All the supported functions should be called as methods, i.e., either as
29
30 $term = new Term::ReadLine 'name';
31
32or as
33
34 $term->addhistory('row');
35
1fef88e7 36where $term is a return value of Term::ReadLine-E<gt>Init.
cb31d310 37
38=over 12
39
40=item C<ReadLine>
41
42returns the actual package that executes the commands. Among possible
43values are C<Term::ReadLine::Gnu>, C<Term::ReadLine::Perl>,
44C<Term::ReadLine::Stub Exporter>.
45
46=item C<new>
47
48returns the handle for subsequent calls to following
49functions. Argument is the name of the application. Optionally can be
50followed by two arguments for C<IN> and C<OUT> filehandles. These
51arguments should be globs.
52
53=item C<readline>
54
55gets an input line, I<possibly> with actual C<readline>
56support. Trailing newline is removed. Returns C<undef> on C<EOF>.
57
58=item C<addhistory>
59
60adds the line to the history of input, from where it can be used if
61the actual C<readline> is present.
62
63=item C<IN>, $C<OUT>
64
65return the filehandles for input and output or C<undef> if C<readline>
66input and output cannot be used for Perl.
67
68=item C<MinLine>
69
70If argument is specified, it is an advice on minimal size of line to
71be included into history. C<undef> means do not include anything into
72history. Returns the old value.
73
74=item C<findConsole>
75
76returns an array with two strings that give most appropriate names for
1fef88e7 77files for input and output using conventions C<"E<lt>$in">, C<"E<gt>out">.
cb31d310 78
a737e074 79=item Attribs
80
81returns a reference to a hash which describes internal configuration
82of the package. Names of keys in this hash conform to standard
83conventions with the leading C<rl_> stripped.
84
cb31d310 85=item C<Features>
86
87Returns a reference to a hash with keys being features present in
88current implementation. Several optional features are used in the
89minimal interface: C<appname> should be present if the first argument
90to C<new> is recognized, and C<minline> should be present if
91C<MinLine> method is not dummy. C<autohistory> should be present if
92lines are put into history automatically (maybe subject to
93C<MinLine>), and C<addhistory> if C<addhistory> method is not dummy.
94
a737e074 95If C<Features> method reports a feature C<attribs> as present, the
96method C<Attribs> is not dummy.
97
cb31d310 98=back
99
a737e074 100=head1 Additional supported functions
101
cb31d310 102Actually C<Term::ReadLine> can use some other package, that will
103support reacher set of commands.
104
a737e074 105All these commands are callable via method interface and have names
106which conform to standard conventions with the leading C<rl_> stripped.
107
cb31d310 108=head1 EXPORTS
109
110None
111
a737e074 112=head1 ENVIRONMENT
113
114The variable C<PERL_RL> governs which ReadLine clone is loaded. If the
115value is false, a dummy interface is used. If the value is true, it
116should be tail of the name of the package to use, such as C<Perl> or
117C<Gnu>.
118
119If the variable is not set, the best available package is loaded.
120
cb31d310 121=cut
122
123package Term::ReadLine::Stub;
a737e074 124@ISA = 'Term::ReadLine::Tk';
cb31d310 125
126$DB::emacs = $DB::emacs; # To peacify -w
127
128sub ReadLine {'Term::ReadLine::Stub'}
129sub readline {
a737e074 130 my $self = shift;
131 my ($in,$out,$str) = @$self;
cb31d310 132 print $out shift;
a737e074 133 $self->register_Tk
134 if not $Term::ReadLine::registered and $Term::ReadLine::toloop
135 and defined &Tk::DoOneEvent;
136 #$str = scalar <$in>;
137 $str = $self->get_line;
cb31d310 138 # bug in 5.000: chomping empty string creats length -1:
139 chomp $str if defined $str;
140 $str;
141}
142sub addhistory {}
143
144sub findConsole {
145 my $console;
146
147 if (-e "/dev/tty") {
148 $console = "/dev/tty";
149 } elsif (-e "con") {
150 $console = "con";
151 } else {
152 $console = "sys\$command";
153 }
154
155 if (defined $ENV{'OS2_SHELL'}) { # In OS/2
156 if ($DB::emacs) {
157 $console = undef;
158 } else {
159 $console = "/dev/con";
160 }
161 }
162
163 $consoleOUT = $console;
164 $console = "&STDIN" unless defined $console;
165 if (!defined $consoleOUT) {
166 $consoleOUT = defined fileno(STDERR) ? "&STDERR" : "&STDOUT";
167 }
168 ($console,$consoleOUT);
169}
170
171sub new {
172 die "method new called with wrong number of arguments"
173 unless @_==2 or @_==4;
174 #local (*FIN, *FOUT);
175 my ($FIN, $FOUT);
176 if (@_==2) {
177 ($console, $consoleOUT) = findConsole;
178
179 open(FIN, "<$console");
180 open(FOUT,">$consoleOUT");
181 #OUT->autoflush(1); # Conflicts with debugger?
182 $sel = select(FOUT);
183 $| = 1; # for DB::OUT
184 select($sel);
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
191 select($sel);
192 bless [$FIN, $FOUT];
193 }
194}
195sub IN { shift->[0] }
196sub OUT { shift->[1] }
197sub MinLine { undef }
a737e074 198sub Attribs { {} }
199
200my %features = (tkRunning => 1);
201sub Features { \%features }
cb31d310 202
203package Term::ReadLine; # So late to allow the above code be defined?
a737e074 204
205my $which = $ENV{PERL_RL};
206if ($which) {
207 if ($which =~ /\bgnu\b/i){
208 eval "use Term::ReadLine::Gnu;";
209 } elsif ($which =~ /\bperl\b/i) {
210 eval "use Term::ReadLine::Perl;";
211 } else {
212 eval "use Term::ReadLine::$which;";
213 }
214} elsif (defined $which) { # Defined but false
215 # Do nothing fancy
216} else {
217 eval "use Term::ReadLine::Gnu; 1" or eval "use Term::ReadLine::Perl; 1";
218}
cb31d310 219
220#require FileHandle;
221
222# To make possible switch off RL in debugger: (Not needed, work done
223# in debugger).
224
225if (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);
229} else {
230 @ISA = qw(Term::ReadLine::Stub);
231}
232
a737e074 233package Term::ReadLine::Tk;
234
235$count_handle = $count_DoOne = $count_loop = 0;
236
237sub handle {$giveup = 1; $count_handle++}
238
239sub Tk_loop {
240 # Tk->tkwait('variable',\$giveup); # needs Widget
241 $count_DoOne++, Tk::DoOneEvent(0) until $giveup;
242 $count_loop++;
243 $giveup = 0;
244}
245
246sub register_Tk {
247 my $self = shift;
248 $Term::ReadLine::registered++
249 or Tk->fileevent($self->IN,'readable',\&handle);
250}
251
252sub tkRunning {
253 $Term::ReadLine::toloop = $_[1] if @_ > 1;
254 $Term::ReadLine::toloop;
255}
256
257sub get_c {
258 my $self = shift;
259 $self->Tk_loop if $Term::ReadLine::toloop && defined &Tk::DoOneEvent;
260 return getc $self->IN;
261}
262
263sub get_line {
264 my $self = shift;
265 $self->Tk_loop if $Term::ReadLine::toloop && defined &Tk::DoOneEvent;
266 my $in = $self->IN;
267 return scalar <$in>;
268}
cb31d310 269
2701;
271