lib/Term/ReadLine/Perl5.pm

filter: Statementstmt 77.7% 91 / 117 Branchbran 44.4% 16 / 36 Conditioncond 27.7% 5 / 18 MC/DCmcdc 9.0% 1 / 11 Subroutinesub 75.0% 15 / 20 Podpod 87.5% 7 / 8 totaltotal 64.2% 135 / 210 CC 23 SCAR 35.3
CC
23
Cov
65%
new
73.2
MinLine
6.0
stifle_history
4.0
CRAP 34.3
« blib/lib/Term/ReadLine/Perl5/OO.pm ↑ Summary lib/Term/ReadLine/Perl5/OO/Keymap.pm »
1 # -*- Perl -*-
2 package Term::ReadLine::Perl5;
3 =encoding utf8
4  
5 =head1 Name
6  
7 Term::ReadLine::Perl5 - A Perl5 implementation GNU Readline
8  
9 =head2 Overview
10  
11 This is a implementation of the GNU Readline/History Library written
12 in Perl5.
13  
14 GNU Readline reads lines from an interactive terminal with I<emacs> or
15 I<vi> editing capabilities. It provides as mechanism for saving
16 history of previous input.
17  
18 This package typically used in command-line interfaces and REPLs (Read,
19 Eval, Print, Loop).
20  
21 =head2 Demo program
22  
23 Another package, L<Term::ReadLine::Perl5::Demo> is available to let
24 you run I<Term::ReadLine::Perl5> to experiment with its capabilities
25 and show how to use the API.
26  
27 =head1 Synopsis
28  
29 use Term::ReadLine::Perl5;
30 $term = Term::ReadLine::Perl5->new('ProgramName');
31 while ( defined ($_ = $term->readline('prompt>')) ) {
32 ...
33 }
34  
35 =cut
36 8 ▶use warnings; use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
37 8 ▶use Term::ReadLine::Perl5::readline;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 8 ▶no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 8 our $VERSION = '1.42';
41  
42 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 8 ▶eval "use rlib '.' "; # rlib is now optional
Subroutine BEGIN: called CC=1 SCAR=0.0
44 8 ▶use Term::ReadLine::Perl5::History;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 8 ▶use Term::ReadLine::Perl5::OO;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 8 ▶use Term::ReadLine::Perl5::OO::History;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 8 ▶use Term::ReadLine::Perl5::Tie;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 8 ▶use Term::ReadLine::Perl5::readline;
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 8 ▶if (require Term::ReadLine) {
Branch: 1 branch
BranchTrueFalseTotal
if (require Term::ReadLine) 8 0 8
51 8 our @ISA = qw(Term::ReadLine::Stub);
52 }
53 8 my (%attribs, $term);
54  
55 =head2 Variables
56  
57 Following GNU Readline/History Library variables can be accessed from
58 Perl program. See 'GNU Readline Library Manual' and ' GNU History
59 Library Manual' for each variable. You can access them via the
60 C<Attribs> method. Names of keys in this hash conform to standard
61 conventions with the leading C<rl_> stripped.
62  
63 Example:
64  
65 $term = Term::ReadLine::Perl5->new('ReadLineTest');
66 $attribs = $term->Attribs;
67 $v = $attribs->{history_base}; # history_base
68  
69 =head3 Attribute Names
70  
71 completion_suppress_append (bool)
72 history_base (int)
73 history_stifled (int)
74 max_input_history (int)
75 outstream (file handle)
76  
77 =cut
78  
79 8 my %features = (
80 appname => 1, # "new" is recognized
81 minline => 1, # we have a working MinLine()
82 autohistory => 1, # lines are put into history automatically,
83 # subject to MinLine()
84 getHistory => 1, # we have a working getHistory()
85 setHistory => 1, # we have a working setHistory()
86 addHistory => 1, # we have a working add_history(), addhistory(),
87 # or addHistory()
88 readHistory => 1, # we have read_history() or readHistory()
89 writeHistory => 1, # we have writeHistory()
90 preput => 1, # the second argument to readline is processed
91 attribs => 1,
92 newTTY => 1, # we have newTTY()
93 stiflehistory => 1, # we have stifle_history()
94 );
95  
96 8 ▶tie %attribs, 'Term::ReadLine::Perl5::Tie' or die ;
Branch: 1 branch
BranchTrueFalseTotal
tie %attribs, "Term::ReadLine::Perl5::Tie" or die 0 8 8
97 sub Attribs {
98 2 ▶ \%attribs;
Subroutine Attribs: called CC=1 SCAR=0.0
Pod undocumented
99 }
100  
101 =head1 Subroutine
102  
103 =head2 Standard Term::ReadLine Methods
104  
105 These methods are standard methods defined by
106 L<Term::ReadLine>.
107  
108 =head3 C<ReadLine>
109  
110 Readline() -> 'Term::ReadLine::Perl5'
111  
112 returns the actual package that executes the commands. If this package
113 is used, the value is C<Term::ReadLine::Perl5>.
114  
115 =cut
116 1 ▶sub ReadLine {'Term::ReadLine::Perl5'}
Subroutine ReadLine: called CC=1 SCAR=0.0
Pod documented
117  
118  
119 =head3 readline
120  
121 $bool = $term->readline($prompt, $default)
122  
123 The main routine to call interactively read lines. Parameter
124 I<$prompt> is the text you want to prompt with If it is empty string,
125 no preceding prompt text is given. It is I<undef> a default value of
126 "INPUT> " is used.
127  
128 Parameter I<$default> is the default value; it can be can be
129 omitted. The next input line is returned or I<undef> on EOF.
130  
131 =cut
132  
133 sub readline {
134 0 ▶ shift;
Subroutine readline: not called CC=1 SCAR=6.9
Pod documented
135 0 &Term::ReadLine::Perl5::readline::readline(@_);
136 }
137  
138 =head3 new
139  
140 B<new>(I<$name>,[I<IN>[,I<OUT>]])
141  
142 returns the handle for subsequent calls to following functions.
143 Argument is the name of the application. Optionally can be followed
144 by two arguments for C<IN> and C<OUT> file handles. These arguments
145 should be globs.
146  
147 I<$name> is the name of the application.
148  
149 This routine may also get called via
150 C<Term::ReadLine-E<gt>new($term_name)> if you have
151 C<$ENV{PERL_RL}> set to 'Perl5';
152  
153 At present, because this code has lots of global state, we currently don't
154 support more than one readline instance.
155  
156 =cut
157 sub new {
158 1 ▶ my $class = shift;
Subroutine new: called CC=19 SCAR=42.9
Pod documented
159 1 ▶ if (require Term::ReadLine) {
Branch: 1 branch
BranchTrueFalseTotal
if (require Term::ReadLine) 1 0 1
160 1 $features{tkRunning} = Term::ReadLine::Stub->Features->{'tkRunning'};
161 1 $features{ornaments} = Term::ReadLine::Stub->Features->{'ornaments'};
162 }
163 1 ▶ if (defined $term) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $term) 0 1 1
164 0 my $stderr = $Term::ReadLine::Perl5::readline::term_OUT;
165 0 print $stderr "Cannot create second readline interface\n";
166 0 print "Using experimental OO interface based on Caroline\n";
167 0 my ($name, $in, $out) = @_;
168 0 my $opts = {
169 name => $name,
170 in => $in,
171 out => $out,
172 };
173 0 return Term::ReadLine::Perl5::OO->new($opts);
174 }
175 1 shift; # Package name
176 1 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 1 1
177 0 ▶ if ($term) {
Branch: 1 branch
BranchTrueFalseTotal
if ($term) { } 0 0 0
178 0 ▶ warn "Ignoring name of second readline interface.\n"
Branch: 1 branch
BranchTrueFalseTotal
if defined $term 0 0 0
179 if defined $term;
180 0 shift;
181 } else {
182 # Set Name
183 0 $Term::ReadLine::Perl5::readline::rl_readline_name = shift;
184 }
185 }
186 1 ▶ if (!@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @_) { } 1 0 1
187 1 ▶ if (!defined $term) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $term) 1 0 1
188 1 my ($IN,$OUT) = Term::ReadLine->findConsole();
189 # Old Term::ReadLine did not have a workaround for a bug
190 # in Win devdriver
191 1 ▶ $IN = 'CONIN$' if $^O eq 'MSWin32' and "\U$IN" eq 'CON';
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "MSWin32" and uc $IN eq "CON" 0 1 1
Condition33%1/3
$^O eq "MSWin32" and uc $IN eq "CON"
!ll&&!rl&&r
100
MC/DC0%0/2
$^O eq "MSWin32" and uc $IN eq "CON"
$^O eq "MSWin32" uc $IN eq "CON"
Truth table33%1/3
A and B
A= $^O eq "MSWin32"B= uc $IN eq "CON"
ABresult
0X0
100
111
192 1 ▶ open(my $in_fh,
Branch: 2 branches
BranchTrueFalseTotal
open my $in_fh, $IN eq "CONIN\$" && $^O eq "MSWin32" ? "+< $IN" : "< $IN" or croak("Cannot open $IN for read") 0 1 1
$IN eq 'CONIN$' && $^O eq 'MSWin32' ? : 0 1 1
Condition33%1/3
$IN eq "CONIN\$" and $^O eq "MSWin32"
!ll&&!rl&&r
100
MC/DC0%0/2
$IN eq "CONIN\$" and $^O eq "MSWin32"
$IN eq "CONIN\$" $^O eq "MSWin32"
Truth table33%1/3
A and B
A= $IN eq "CONIN\$"B= $^O eq "MSWin32"
ABresult
0X0
100
111
193 # A workaround for another bug in Win device driver
194 (($IN eq 'CONIN$' and $^O eq 'MSWin32') ? "+< $IN" : "< $IN"))
195 or croak "Cannot open $IN for read";
196 1 ▶ open(my $out_fh, ">$OUT") || croak "Cannot open $OUT for write: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $out_fh, ">$OUT" or croak("Cannot open $OUT for write: $!") 0 1 1
197 1 $Term::ReadLine::Perl5::readline::term_IN = $in_fh;
198 1 $Term::ReadLine::Perl5::readline::term_OUT = $out_fh;
199 }
200 } else {
201 0 ▶ if (defined $term and ($term->IN ne $_[0] or $term->OUT ne $_[1]) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $term and $term->IN ne $_[0] || $term->OUT ne $_[1]) 0 0 0
Condition0%0/6
defined $term and $term->IN ne $_[0] || $term->OUT ne $_[1]
!ll&&!rl&&r
000
$term->IN ne $_[0] or $term->OUT ne $_[1]
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $term and $term->IN ne $_[0] || $term->OUT ne $_[1]
defined $term $term->IN ne $_[0] $term->OUT ne $_[1]
Truth table0%0/4
A and B or C
A= defined $termB= $term->IN ne $_[0]C= $term->OUT ne $_[1]
ABCresult
0XX0
1000
11X1
1011
202 0 croak "Request for a second readline interface with different terminal";
203 }
204 0 $Term::ReadLine::Perl5::readline::term_IN = shift;
205 0 $Term::ReadLine::readline::term_OUT = shift
206 }
207 # The following is here since it is mostly used for perl input:
208 # $readline::rl_basic_word_break_characters .= '-:+/*,[])}';
209 1 $term = bless [$readline::term_IN,$readline::term_OUT];
210 1 my $self = {
211 'IN' => $readline::term_IN,
212 'OUT' => $readline::term_OUT,
213 };
214 1 bless $self, $class;
215  
216 1 ▶ unless ($ENV{PERL_RL} and $ENV{PERL_RL} =~ /\bo\w*=0/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ENV{"PERL_RL"} and $ENV{"PERL_RL"} =~ /\bo\w*=0/) 1 0 1
Condition33%1/3
$ENV{"PERL_RL"} and $ENV{"PERL_RL"} =~ /\bo\w*=0/
!ll&&!rl&&r
010
MC/DC0%0/2
$ENV{"PERL_RL"} and $ENV{"PERL_RL"} =~ /\bo\w*=0/
$ENV{"PERL_RL"} $ENV{"PERL_RL"} =~ /\bo\w*=0/
Truth table33%1/3
A and B
A= $ENV{"PERL_RL"}B= $ENV{"PERL_RL"} =~ /\bo\w*=0/
ABresult
0X0
100
111
217 1 local $Term::ReadLine::termcap_nowarn = 1; # With newer Perls
218 1 ▶ local $SIG{__WARN__} = sub {}; # With older Perls
Subroutine __ANON__: not called CC=1 SCAR=0.0
219 1 $term->ornaments(1);
220 }
221  
222 # FIXME: something rl_term_set in here causes terminal attributes
223 # like bold and underline to work.
224 1 Term::ReadLine::Perl5::readline::rl_term_set();
225  
226 1 return $self;
227 }
228  
229 =head3 newTTY
230  
231 B<Term::ReadLine::Perl5-E<gt>newTTY>(I<IN>, I<OUT>)
232  
233 takes two arguments which are input filehandle and output filehandle.
234 Switches to use these filehandles.
235 =cut
236 sub newTTY($$$) {
237 0 ▶ my ($self, $in, $out) = @_;
Subroutine newTTY: not called CC=1 SCAR=6.9
Pod documented
238 0 $Term::ReadLine::Perl5::readline::term_IN = $self->{'IN'} = $in;
239 0 $Term::ReadLine::Perl5::readline::term_OUT = $self->{'OUT'} = $out;
240 0 my $sel = select($out);
241 0 $| = 1; # for DB::OUT
242 0 select($sel);
243 }
244  
245 =head3 Minline
246  
247 B<MinLine>([I<$minlength>])>
248  
249 If B<$minlength> is given, set C<$readline::minlength> the minimum
250 length a $line for it to go into the readline history.
251  
252 The previous value is returned.
253 =cut
254 sub MinLine($;$) {
255 0 ▶ my $old = $minlength;
Subroutine MinLine: not called CC=2 SCAR=17.9
Pod documented
256 0 ▶ $minlength = $_[1] if @_ == 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 2 0 0 0
257 0 return $old;
258 }
259  
260 #################### History ##########################################
261  
262 =head3 add_history
263  
264 $term->add_history>($line1, $line2, ...)
265  
266 adds the lines, I<$line1>, etc. to the input history list.
267  
268 I<AddHistory> is an alias for this function.
269  
270 =cut
271  
272 # GNU ReadLine names
273 8 *add_history = \&Term::ReadLine::Perl5::History::add_history;
274 8 *remove_history = \&Term::ReadLine::Perl5::History::remove_history;
275 8 *replace_history_entry = \&Term::ReadLine::Perl5::History::replace_history_entry;
276  
277 8 *clear_history = \&Term::ReadLine::Perl5::History::clear_history;
278  
279 8 *history_is_stifled = \&Term::ReadLine::Perl5::History::history_is_stifled;
280 8 *read_history = \&Term::ReadLine::Perl5::History::read_history;
281 8 *unstifle_history = \&Term::ReadLine::Perl5::History::unstifle_history;
282 8 *write_history = \&Term::ReadLine::Perl5::History::write_history;
283  
284 # Not sure about the difference between history_list and GetHistory.
285 8 *history_list = \&Term::ReadLine::Perl5::OO::GetHistory;
286  
287 8 *rl_History = *Term::ReadLine::Perl5::rl_History;
288  
289  
290 # Some Term::ReadLine::Gnu names
291 8 *AddHistory = \&add_history;
292 8 *GetHistory = \&Term::ReadLine::Perl5::History::GetHistory;
293 8 *SetHistory = \&Term::ReadLine::Perl5::History::SetHistory;
294 8 *ReadHistory = \&Term::ReadLine::Perl5::History::ReadHistory;
295 8 *WriteHistory = \&Term::ReadLine::Perl5::History::WriteHistory;
296  
297 # Backward compatibility:
298 8 *addhistory = \&add_history;
299 8 *StifleHistory = \&stifle_history;
300  
301 =head3 stifle_history
302  
303 $term->stifle_history($max)
304  
305 Stifle or put a cap on the history list, remembering only C<$max>
306 number of lines.
307  
308 I<StifleHistory> is an alias for this function.
309  
310 =cut
311 ### FIXME: stifle_history is still here because it updates $attribs.
312 ## Pass a reference?
313 sub stifle_history($$) {
314 3 ▶ my ($self, $max) = @_;
Subroutine stifle_history: called CC=4 SCAR=13.9
Pod documented
315 3 ▶ $max = 0 if !defined($max) || $max < 0;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $max or $max < 0 1 2 3
Condition66%2/3
not defined $max or $max < 0
l!l&&r!l&&!r
102
MC/DC50%1/2
not defined $max or $max < 0
not defined $max $max < 0
Truth table66%2/3
not(A) or B
A= not defined $maxB= $max < 0
ABresult
1X1
011
000
316  
317 3 ▶ if (scalar @rl_History > $max) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @Term::ReadLine::Perl5::rl_History > $max) 2 1 3
318 2 splice @rl_History, $max;
319 2 $attribs{history_length} = scalar @rl_History;
320 }
321  
322 3 $Term::ReadLine::Perl5::History::history_stifled = 1;
323 3 $attribs{max_input_history} = $self->{rl_max_input_history} = $max;
324 }
325  
326 =head3 Features
327  
328 B<Features()>
329  
330 Returns a reference to a hash with keys being features present in
331 current implementation. Several optional features are used in the
332 minimal interface:
333  
334 =over
335  
336 =item *
337 I<addHistory> is present if you can add lines to history list via
338 the I<addHistory()> method
339  
340 =item *
341 I<appname> is be present if a name, the first argument
342 to I<new()> was given
343  
344 =item *
345 I<autohistory> is present if lines are put into history automatically
346 subject to the line being longer than I<MinLine>.
347  
348 =item *
349 I<getHistory> is present if we get retrieve history via the I<getHistory()>
350 method
351  
352 =item *
353 I<minline> is present if the I<MinLine> method available.
354  
355 =item *
356 I<preput> is present if the second argument to I<readline> method can
357 append text to the input to be read subsequently
358  
359 =item *
360 I<readHistory> is present you can read history
361 items previosly saved in a file.
362  
363 =item *
364 I<setHistory> is present if we can set history
365  
366 =item *
367 I<stifleHistory> is present you can put a limit of the nubmer of history
368 items to save via the I<writeHistory()> method
369  
370 =item *
371 I<tkRunning> is present if a Tk application may run while I<ReadLine> is
372 getting input.
373  
374 =item *
375 I<writeHistory> is present you can save history to a file via the
376 I<writeHistory()> method
377  
378 =back
379  
380 =cut
381  
382 0 ▶sub Features { \%features; }
Subroutine Features: not called CC=1 SCAR=6.9
Pod documented
383  
384 =head1 See also
385  
386 =over
387  
388 =item *
389  
390 L<Term::ReadLine::Perl5::OO> is the newer but unfinished fully OO version.
391  
392 =item *
393  
394 L<Term::ReadLine::Perl5> is the first try at the OO package that most
395 programmers will use.
396  
397 =item *
398  
399 L<Term::ReadLine::Perl5::readline-guide> is guide to the guts of the
400 non-OO portion of L<Term::ReadLine::Perl5>
401  
402 =item *
403  
404 L<Term::ReadLine::Perl5::History> describes the history
405 mechanism
406  
407 =item *
408  
409 L<Term::ReadLine> is a generic package which can be used to
410 select this among other compatible GNU Readline packages.
411  
412 =back
413  
414 =cut
415 8 1;
« blib/lib/Term/ReadLine/Perl5/OO.pm ↑ Summary lib/Term/ReadLine/Perl5/OO/Keymap.pm »