File Coverage

lib/Devel/Trepan/IO/Input.pm
Criterion Covered Total %
statement 54 82 65.8
branch 7 32 21.8
condition 7 15 46.6
subroutine 16 19 84.2
pod 0 8 0.0
total 84 156 53.8


line stmt bran cond sub pod time code
1             # -*- coding: utf-8 -*-
2             # Copyright (C) 2011-2015 Rocky Bernstein <rocky@cpan.org>
3              
4             # Debugger user/command-oriented input possibly attached to IO-style
5             # input or GNU Readline.
6             #
7              
8 16     16   24064 use warnings; use strict;
  16     16   46  
  16         526  
  16         108  
  16         42  
  16         445  
9 15     15   80 use Exporter;
  15         35  
  15         796  
10              
11             package Devel::Trepan::IO::Input;
12              
13 15     15   99 use vars qw(@EXPORT @ISA $HAVE_TERM_READLINE);
  15         35  
  15         1525  
14             @ISA = qw(Devel::Trepan::IO::InputBase Exporter);
15             @EXPORT = qw($HAVE_TERM_READLINE term_readline_capability);
16              
17             BEGIN {
18 15     15   112 my @OLD_INC = @INC;
19 15     15   99 use rlib '../../..';
  15         39  
  15         94  
20 15     15   6421 use Devel::Trepan::Util qw(hash_merge);
  15         43  
  15         849  
21 15     15   5440 use Devel::Trepan::IO;
  15         50  
  15         493  
22 15         519 @INC = @OLD_INC;
23             }
24              
25             END {
26 15 50   15   1844 if ($HAVE_TERM_READLINE eq 'Perl5') {
27 15     15   99 no strict 'subs';
  15         37  
  15         14568  
28 15         134 Term::ReadLine::Perl5::readline::ResetTTY;
29             }
30             }
31              
32             sub term_readline_capability() {
33             # Prefer Term::ReadLine::Perl5 if we have it
34             return 'Perl5' if
35 15 50 66 15 0 7996 (!$ENV{PERL_RL} || $ENV{PERL_RL} =~ /\bperl5\b/i) &&
  15   33 17   1679177  
  15         963  
  17         2836  
36             eval q(use Term::ReadLine::Perl5; 1);
37              
38 0 0       0 if ($ENV{PERL_RL}) {
39 0         0 return eval q(use Term::ReadLine; 1);
40             } else {
41             # Prefer Term::ReadLine::Perl for Term::ReadLine
42 0         0 foreach my $ilk (qw(Perl Gnu)) {
43 0 0       0 return $ilk if eval qq(use Term::ReadLine::$ilk; 1);
44             }
45 0 0       0 return 'Stub' if eval q(use Term::ReadLine; 1);
46             }
47 0         0 return 0;
48             }
49              
50             $HAVE_TERM_READLINE = term_readline_capability();
51              
52             sub new($;$$) {
53 27     27 0 697 my ($class, $inp, $opts) = @_;
54 27   66     227 $inp ||= *STDIN;
55 27         200 my $self = Devel::Trepan::IO::InputBase->new($inp, $opts);
56 27         128 $self->{histfile} = undef;
57 27 100 66     183 if ($opts->{readline} && $HAVE_TERM_READLINE) {
58 13         34 my $rc = 0;
59 13         39 $rc = eval {
60             ## FIXME: Simplify after Term::ReadLine::Perl5 is in Term::ReadLine
61 13 50       107 if ($HAVE_TERM_READLINE eq 'Perl5') {
62 13         157 $self->{readline} = Term::ReadLine::Perl5->new('trepan.pl');
63             } else {
64 0         0 $self->{readline} = Term::ReadLine->new('trepan.pl');
65             }
66 13         187673 1 ;
67             };
68 13 50       105 if ($rc) {
69 13         80 $self->{term_readline} = 1;
70             } else {
71 0         0 $self->{readline} = undef;
72 0         0 $self->{term_readline} = 0;
73             }
74             } else {
75 14         42 $self->{readline} = undef;
76 14         45 $self->{term_readline} = 0;
77             }
78 27         528 bless ($self, $class);
79 27         5456 return $self;
80             }
81              
82             sub have_term_readline($)
83             {
84 1     1 0 2 my $self = shift;
85 1 50 0     6 $self->{term_readline} && (exists($ENV{'TERM'}) && $ENV{'TERM'} ne 'dumb');
86             }
87              
88             sub want_term_readline($)
89             {
90 1     1 0 7 my $self = shift;
91 1         5 $self->{term_readline};
92             }
93              
94             sub is_interactive($) {
95 1     1 0 6 my $self = shift;
96 1         10 return -t $self->{input};
97             }
98              
99             sub rl_filename_list($$) {
100 0     0 0   my ($self, $prefix) = @_;
101 0 0         if ($HAVE_TERM_READLINE eq 'Perl5') {
    0          
102 0           Term::ReadLine::Perl5::readline::rl_filename_list($prefix);
103             } elsif ($HAVE_TERM_READLINE eq 'Gnu') {
104 0           eval {Term::ReadLine::Gnu::XS::rl_filename_list($prefix)};
  0            
105             # } elsif ($HAVE_TERM_READLINE eq 'Perl') {
106             # FIXME: how does one do this for Perl?
107             }
108             }
109              
110             # Read a line of input. EOFError will be raised on EOF.
111             # Prompt is ignored if we don't have GNU readline. In that
112             # case, it should have been handled prior to this call.
113             sub readline($;$) {
114 0     0 0   my ($self, $prompt) = @_;
115 0 0         $prompt = '' unless defined($prompt);
116 0           my $line;
117 0 0         if (defined $self->{readline}) {
118 0           $line = $self->{readline}->readline($prompt);
119 0           $self->{eof} = !defined($line);
120             } else {
121 0           $self->{eof} = eof($self->{input});
122 0 0         return '' if $self->{eof};
123 0           $line = CORE::readline $self->{input};
124             }
125 0           return $line;
126             }
127              
128             sub write_history($$)
129             {
130 0     0 0   my ($self, $histfile) = @_;
131             $self->{readline}->StifleHistory($self->{histsize}) if
132 0 0         $self->{readline}->can("StifleHistory");
133 0 0         if ($self->{readline}->can("WriteHistory")) {
134             $self->{readline}->WriteHistory($self->{histfile})
135 0           }
136             }
137              
138             # Demo
139             unless (caller) {
140             my $in = __PACKAGE__->new(*main::STDIN, {line_edit => 1});
141             require Data::Dumper; import Data::Dumper;
142             print Dumper($in), "\n";
143             printf "Is interactive: %s\n", ($in->is_interactive ? "yes" : "no");
144             printf "term_readline_capability: %s\n", term_readline_capability();
145             printf "Have Term::ReadLine: %s\n", ($HAVE_TERM_READLINE ? "yes" : "no");
146             if (scalar(@ARGV) > 0) {
147             print "Enter some text: ";
148             my $line = $in->readline;
149             if ($in->is_eof) {
150             print "EOF seen\n";
151             } else {
152             print "You entered ${line}";
153             }
154             }
155             my $inp = __PACKAGE__->new(undef, {readline => 0});
156             printf "Input open has Term::ReadLine: %s\n", ($inp->want_term_readline ? "yes" : "no");
157             $inp = __PACKAGE__->new(undef, {readline => 1});
158             printf "Input open now has Term::ReadLine: %s\n", ($inp->want_term_readline ? "yes" : "no");
159             foreach my $term (qw(Gnu Perl5)) {
160             $HAVE_TERM_READLINE = $term;
161             my $path='./';
162             my @files = $inp->rl_filename_list($path);
163             printf "term: %s, path: %s\n", $term, $path;
164             foreach my $file (@files) {
165             print "\t$file\n";
166             }
167             }
168             }
169             1;