1Term::ReadLine(3pm) Perl Programmers Reference Guide Term::ReadLine(3pm)
2
3
4
6 Term::ReadLine - Perl interface to various "readline" packages. If no
7 real package is found, substitutes stubs instead of basic functions.
8
10 use Term::ReadLine;
11 my $term = Term::ReadLine->new('Simple Perl calc');
12 my $prompt = "Enter your arithmetic expression: ";
13 my $OUT = $term->OUT || \*STDOUT;
14 while ( defined ($_ = $term->readline($prompt)) ) {
15 my $res = eval($_);
16 warn $@ if $@;
17 print $OUT $res, "\n" unless $@;
18 $term->addhistory($_) if /\S/;
19 }
20
22 This package is just a front end to some other packages. It's a stub to
23 set up a common interface to the various ReadLine implementations found
24 on CPAN (under the "Term::ReadLine::*" namespace).
25
27 All the supported functions should be called as methods, i.e., either
28 as
29
30 $term = Term::ReadLine->new('name');
31
32 or as
33
34 $term->addhistory('row');
35
36 where $term is a return value of Term::ReadLine->new().
37
38 "ReadLine" returns the actual package that executes the commands.
39 Among possible values are "Term::ReadLine::Gnu",
40 "Term::ReadLine::Perl", "Term::ReadLine::Stub".
41
42 "new" returns the handle for subsequent calls to following
43 functions. Argument is the name of the application.
44 Optionally can be followed by two arguments for "IN" and
45 "OUT" filehandles. These arguments should be globs.
46
47 "readline" gets an input line, possibly with actual "readline"
48 support. Trailing newline is removed. Returns "undef" on
49 "EOF".
50
51 "addhistory"
52 adds the line to the history of input, from where it can be
53 used if the actual "readline" is present.
54
55 "IN", "OUT" return the filehandles for input and output or "undef" if
56 "readline" input and output cannot be used for Perl.
57
58 "MinLine" If argument is specified, it is an advice on minimal size
59 of line to be included into history. "undef" means do not
60 include anything into history. Returns the old value.
61
62 "findConsole"
63 returns an array with two strings that give most
64 appropriate names for files for input and output using
65 conventions "<$in", ">out".
66
67 Attribs returns a reference to a hash which describes internal
68 configuration of the package. Names of keys in this hash
69 conform to standard conventions with the leading "rl_"
70 stripped.
71
72 "Features" Returns a reference to a hash with keys being features
73 present in current implementation. Several optional
74 features are used in the minimal interface: "appname"
75 should be present if the first argument to "new" is
76 recognized, and "minline" should be present if "MinLine"
77 method is not dummy. "autohistory" should be present if
78 lines are put into history automatically (maybe subject to
79 "MinLine"), and "addhistory" if "addhistory" method is not
80 dummy.
81
82 If "Features" method reports a feature "attribs" as
83 present, the method "Attribs" is not dummy.
84
86 Actually "Term::ReadLine" can use some other package, that will support
87 a richer set of commands.
88
89 All these commands are callable via method interface and have names
90 which conform to standard conventions with the leading "rl_" stripped.
91
92 The stub package included with the perl distribution allows some
93 additional methods:
94
95 "tkRunning" makes Tk event loop run when waiting for user input (i.e.,
96 during "readline" method).
97
98 "event_loop"
99 Registers call-backs to wait for user input (i.e., during
100 "readline" method). This supercedes tkRunning.
101
102 The first call-back registered is the call back for
103 waiting. It is expected that the callback will call the
104 current event loop until there is something waiting to get
105 on the input filehandle. The parameter passed in is the
106 return value of the second call back.
107
108 The second call-back registered is the call back for
109 registration. The input filehandle (often STDIN, but not
110 necessarily) will be passed in.
111
112 For example, with AnyEvent:
113
114 $term->event_loop(sub {
115 my $data = shift;
116 $data->[1] = AE::cv();
117 $data->[1]->recv();
118 }, sub {
119 my $fh = shift;
120 my $data = [];
121 $data->[0] = AE::io($fh, 0, sub { $data->[1]->send() });
122 $data;
123 });
124
125 The second call-back is optional if the call back is
126 registered prior to the call to $term->readline.
127
128 Deregistration is done in this case by calling event_loop
129 with "undef" as its parameter:
130
131 $term->event_loop(undef);
132
133 This will cause the data array ref to be removed, allowing
134 normal garbage collection to clean it up. With AnyEvent,
135 that will cause $data->[0] to be cleaned up, and AnyEvent
136 will automatically cancel the watcher at that time. If
137 another loop requires more than that to clean up a file
138 watcher, that will be up to the caller to handle.
139
140 "ornaments" makes the command line stand out by using termcap data.
141 The argument to "ornaments" should be 0, 1, or a string of
142 a form "aa,bb,cc,dd". Four components of this string
143 should be names of terminal capacities, first two will be
144 issued to make the prompt standout, last two to make the
145 input line standout.
146
147 "newTTY" takes two arguments which are input filehandle and output
148 filehandle. Switches to use these filehandles.
149
150 One can check whether the currently loaded ReadLine package supports
151 these methods by checking for corresponding "Features".
152
154 None
155
157 The environment variable "PERL_RL" governs which ReadLine clone is
158 loaded. If the value is false, a dummy interface is used. If the value
159 is true, it should be tail of the name of the package to use, such as
160 "Perl" or "Gnu".
161
162 As a special case, if the value of this variable is space-separated,
163 the tail might be used to disable the ornaments by setting the tail to
164 be "o=0" or "ornaments=0". The head should be as described above, say
165
166 If the variable is not set, or if the head of space-separated list is
167 empty, the best available package is loaded.
168
169 export "PERL_RL=Perl o=0" # Use Perl ReadLine sans ornaments
170 export "PERL_RL= o=0" # Use best available ReadLine sans ornaments
171
172 (Note that processing of "PERL_RL" for ornaments is in the discretion
173 of the particular used "Term::ReadLine::*" package).
174
175
176
177perl v5.16.3 2013-03-04 Term::ReadLine(3pm)