3 C<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/;
19 =head1 Minimal set of supported functions
21 All the supported functions should be called as methods, i.e., either as
23 $term = new Term::ReadLine 'name';
27 $term->addhistory('row');
29 where $term is a return value of Term::ReadLine->Init.
35 returns the actual package that executes the commands. Among possible
36 values are C<Term::ReadLine::Gnu>, C<Term::ReadLine::Perl>,
37 C<Term::ReadLine::Stub Exporter>.
41 returns the handle for subsequent calls to following
42 functions. Argument is the name of the application. Optionally can be
43 followed by two arguments for C<IN> and C<OUT> filehandles. These
44 arguments should be globs.
48 gets an input line, I<possibly> with actual C<readline>
49 support. Trailing newline is removed. Returns C<undef> on C<EOF>.
53 adds the line to the history of input, from where it can be used if
54 the actual C<readline> is present.
58 return the filehandles for input and output or C<undef> if C<readline>
59 input and output cannot be used for Perl.
63 If argument is specified, it is an advice on minimal size of line to
64 be included into history. C<undef> means do not include anything into
65 history. Returns the old value.
69 returns an array with two strings that give most appropriate names for
70 files for input and output using conventions C<"<$in">, C<"E<gt>out">.
74 Returns a reference to a hash with keys being features present in
75 current implementation. Several optional features are used in the
76 minimal interface: C<appname> should be present if the first argument
77 to C<new> is recognized, and C<minline> should be present if
78 C<MinLine> method is not dummy. C<autohistory> should be present if
79 lines are put into history automatically (maybe subject to
80 C<MinLine>), and C<addhistory> if C<addhistory> method is not dummy.
84 Actually C<Term::ReadLine> can use some other package, that will
85 support reacher set of commands.
93 package Term::ReadLine::Stub;
95 $DB::emacs = $DB::emacs; # To peacify -w
97 sub ReadLine {'Term::ReadLine::Stub'}
99 my ($in,$out,$str) = @{shift()};
102 # bug in 5.000: chomping empty string creats length -1:
103 chomp $str if defined $str;
112 $console = "/dev/tty";
116 $console = "sys\$command";
119 if (defined $ENV{'OS2_SHELL'}) { # In OS/2
123 $console = "/dev/con";
127 $consoleOUT = $console;
128 $console = "&STDIN" unless defined $console;
129 if (!defined $consoleOUT) {
130 $consoleOUT = defined fileno(STDERR) ? "&STDERR" : "&STDOUT";
132 ($console,$consoleOUT);
136 die "method new called with wrong number of arguments"
137 unless @_==2 or @_==4;
138 #local (*FIN, *FOUT);
141 ($console, $consoleOUT) = findConsole;
143 open(FIN, "<$console");
144 open(FOUT,">$consoleOUT");
145 #OUT->autoflush(1); # Conflicts with debugger?
147 $| = 1; # for DB::OUT
149 bless [\*FIN, \*FOUT];
150 } else { # Filehandles supplied
151 $FIN = $_[2]; $FOUT = $_[3];
152 #OUT->autoflush(1); # Conflicts with debugger?
153 $sel = select($FOUT);
154 $| = 1; # for DB::OUT
159 sub IN { shift->[0] }
160 sub OUT { shift->[1] }
161 sub MinLine { undef }
164 package Term::ReadLine; # So late to allow the above code be defined?
165 eval "use Term::ReadLine::Gnu;" or eval "use Term::ReadLine::Perl;";
169 # To make possible switch off RL in debugger: (Not needed, work done
172 if (defined &Term::ReadLine::Gnu::readline) {
173 @ISA = qw(Term::ReadLine::Gnu Term::ReadLine::Stub);
174 } elsif (defined &Term::ReadLine::Perl::readline) {
175 @ISA = qw(Term::ReadLine::Perl Term::ReadLine::Stub);
177 @ISA = qw(Term::ReadLine::Stub);