Pager.pm

filter: Statementstmt 71.7% 114 / 159 Branchbran 48.5% 33 / 68 Conditioncond 34.0% 16 / 47 MC/DCmcdc 11.5% 3 / 26 Subroutinesub 70.9% 22 / 31 Podpod 60.0% 3 / 5 totaltotal 56.8% 191 / 336 SCAR 55.1
CC
54
Cov
59%
BINMODE
42.0
BEGIN
21.5
find_pager
14.0
CRAP 247.9
« Pager/Perl.pm ↑ Summary Pager/Buffered.pm »
1 package IO::Pager;
2 9 our $VERSION = "2.00"; #Untouched since 1.03
3  
4 9 ▶use 5.008; #At least, for decent perlio, and other modernisms
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use base qw( Tie::Handle );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use Env qw( PAGER );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use PerlIO;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 9 ▶use overload '+' => "PID", bool=> "PID";
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 9 our $SIGPIPE;
16 #use Carp; $SIG{__WARN__} = sub{ print STDERR @_, Carp::longmess(),"\n\n"; };
17  
18 sub find_pager {
19 # Return the name (or path) of a pager that IO::Pager can use
20 15 ▶ my $io_pager;
Subroutine find_pager: called CC=11 SCAR=26.4
Pod undocumented
21  
22 #Permit explicit use of pure perl pager
23 15 local $_ = 'IO::Pager::less';
24 15 ▶ return $_ if (defined($_[0]) && ($_[0] eq $_)) or
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] and $_[0] eq $_ or defined $IO::Pager::PAGER and $IO::Pager::PAGER eq $_ 0 15 15
Condition44%4/9
defined $_[0] and $_[0] eq $_ or defined $IO::Pager::PAGER and $IO::Pager::PAGER eq $_
l!l&&r!l&&!r
0015
defined $_[0] and $_[0] eq $_
!ll&&!rl&&r
1500
defined $IO::Pager::PAGER and $IO::Pager::PAGER eq $_
!ll&&!rl&&r
1050
MC/DC0%0/4
defined $_[0] and $_[0] eq $_ or defined $IO::Pager::PAGER and $IO::Pager::PAGER eq $_
defined $_[0] $_[0] eq $_ defined $IO::Pager::PAGER $IO::Pager::PAGER eq $_
Truth table14%1/7
A and B or C and D
A= defined $_[0]B= $_[0] eq $_C= defined $IO::Pager::PAGERD= $IO::Pager::PAGER eq $_
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
25 (defined($PAGER) && ($PAGER eq $_));
26  
27 # Use File::Which if available (strongly recommended)
28 15 my $which = eval { require File::Which };
29  
30 # Look for pager in PAGER first
31 15 ▶ if ($PAGER) {
Branch: 1 branch
BranchTrueFalseTotal
if ($IO::Pager::PAGER) 4 11 15
32 # Strip arguments e.g. 'less --quiet'
33 4 my ($pager, @options) = (split ' ', $PAGER);
34 4 $pager = _check_pagers([$pager], $which);
35 4 ▶ $io_pager = join ' ', ($pager, @options) if defined $pager;
Branch: 1 branch
BranchTrueFalseTotal
if defined $pager 3 1 4
36 }
37  
38 # Then search pager amongst usual suspects
39 15 ▶ if (not defined $io_pager) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $io_pager) 12 3 15
40 12 my @pagers = ('/etc/alternatives/pager',
41 '/usr/local/bin/less', '/usr/bin/less', '/usr/bin/more');
42 12 $io_pager = _check_pagers(\@pagers, $which)
43 }
44  
45 # Then check PATH for other pagers
46 15 ▶ if ( (not defined $io_pager) && $which ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $io_pager and $which) 0 15 15
Condition33%1/3
not defined $io_pager and $which
!ll&&!rl&&r
1500
MC/DC0%0/2
not defined $io_pager and $which
not defined $io_pager $which
Truth table33%1/3
not(A) and B
A= not defined $io_pagerB= $which
ABresult
0X0
100
111
47 0 my @pagers = ('less', 'most', 'w3m', 'lv', 'pg', 'more');
48 0 $io_pager = _check_pagers(\@pagers, $which );
49 }
50  
51 # If all else fails, default to more (actually IO::Pager::less first)
52 15 ▶ $io_pager ||= 'more';
Condition50%1/2
$io_pager ||= "more"
l!l
150
MC/DC0%0/1
$io_pager ||= "more"
$io_pager
Truth table50%1/2
A
A= $io_pager
Aresult
11
00
53  
54 15 return $io_pager;
55 }
56  
57 sub _check_pagers {
58 16 ▶ my ($pagers, $which) = @_;
Subroutine _check_pagers: called CC=6 SCAR=18.0
59 # Return the first pager in the list that is usable. For each given pager,
60 # given a pager name, try to finds its full path with File::Which if possible.
61 # Given a pager path, verify that it exists.
62 16 my $io_pager = undef;
63 16 for my $pager (@$pagers) {
64 # Get full path
65 16 my $loc;
66 16 ▶ if ( $which && (not File::Spec->file_name_is_absolute($pager)) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($which and not 'File::Spec'->file_name_is_absolute($pager)) { } 2 14 16
Condition66%2/3
$which and not "File::Spec"->file_name_is_absolute($pager)
!ll&&!rl&&r
0142
MC/DC50%1/2
$which and not "File::Spec"->file_name_is_absolute($pager)
$which not "File::Spec"->file_name_is_absolute($pager)
Truth table66%2/3
A and not(B)
A= $whichB= not "File::Spec"->file_name_is_absolute($pager)
ABresult
0X0
100
111
67 2 $loc = File::Which::which($pager);
68 } else {
69 14 $loc = $pager;
70 }
71 # Test that full path is valid (some platforms don't do -x so we use -e)
72 16 ▶ if ( defined($loc) && (-e $loc) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $loc and -e $loc) 15 1 16
Condition66%2/3
defined $loc and -e $loc
!ll&&!rl&&r
1015
MC/DC50%1/2
defined $loc and -e $loc
defined $loc -e $loc
Truth table66%2/3
A and B
A= defined $locB= -e $loc
ABresult
0X0
100
111
73 15 $io_pager = $loc;
74 15 last;
75 }
76 }
77 16 return $io_pager;
78 }
79  
80 #Should have this as first block for clarity, but not with its use of a sub
81 BEGIN { # Set the $ENV{PAGER} to something reasonable
82 9 ▶ our $oldPAGER = $PAGER || '';
Subroutine BEGIN: called CC=7 SCAR=30.7
Condition50%1/2
$IO::Pager::PAGER || ''
l!l
09
MC/DC0%0/1
$IO::Pager::PAGER || ''
$IO::Pager::PAGER
Truth table50%1/2
A
A= $IO::Pager::PAGER
Aresult
11
00
83 9 $PAGER = find_pager();
84
85 9 ▶ if( ($PAGER =~ 'more' and $oldPAGER ne 'more') or
Branch: 1 branch
BranchTrueFalseTotal
if ($IO::Pager::PAGER =~ /more/ and $IO::Pager::oldPAGER ne "more" or $IO::Pager::PAGER eq "IO::Pager::less") 0 9 9
Condition33%2/6
$IO::Pager::PAGER =~ /more/ and $IO::Pager::oldPAGER ne "more" or $IO::Pager::PAGER eq "IO::Pager::less"
l!l&&r!l&&!r
009
$IO::Pager::PAGER =~ /more/ and $IO::Pager::oldPAGER ne "more"
!ll&&!rl&&r
900
MC/DC0%0/3
$IO::Pager::PAGER =~ /more/ and $IO::Pager::oldPAGER ne "more" or $IO::Pager::PAGER eq "IO::Pager::less"
$IO::Pager::PAGER =~ /more/ $IO::Pager::oldPAGER ne "more" $IO::Pager::PAGER eq "IO::Pager::less"
Truth table20%1/5
A and B or C
A= $IO::Pager::PAGER =~ /more/B= $IO::Pager::oldPAGER ne "more"C= $IO::Pager::PAGER eq "IO::Pager::less"
ABCresult
11X1
0X11
1011
0X00
1000
86 $PAGER eq 'IO::Pager::less' ){
87 0 my $io_pager = $PAGER;
88 0 eval "use IO::Pager::less";
89 0 ▶ $PAGER = $io_pager if $@ or not defined $PAGER;
Branch: 1 branch
BranchTrueFalseTotal
if $@ or not defined $IO::Pager::PAGER 0 0 0
Condition0%0/3
$@ or not defined $IO::Pager::PAGER
l!l&&r!l&&!r
000
MC/DC0%0/2
$@ or not defined $IO::Pager::PAGER
$@ not defined $IO::Pager::PAGER
Truth table0%0/3
A or not(B)
A= $@B= not defined $IO::Pager::PAGER
ABresult
1X1
011
000
90 }
91 }
92  
93  
94 #Factory
95 sub open(*;$@) { # FH, [MODE], [CLASS]
96 0 ▶ my $args = {procedural=>1};
Subroutine open: not called CC=3 SCAR=24.8
Pod documented
97 0 ▶ $args->{mode} = splice(@_, 1, 1) if scalar(@_) == 3;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ == 3 0 0 0
98 0 ▶ $args->{subclass} = pop if scalar(@_) == 2;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ == 2 0 0 0
99 0 &new(undef, @_, $args);
100 }
101  
102 #Alternate entrance: drop class but leave FH, subclass
103 sub new(*;$@) { # FH, [MODE], [CLASS]
104 5 ▶ shift;
Subroutine new: called CC=8 SCAR=23.4
Pod documented
105  
106 5 my %args;
107 5 ▶ if( ref($_[-1]) eq 'HASH' ){
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_[-1] eq 'HASH') { } 0 5 5
elsif (defined $_[1]) { } 2 3 5
108 0 %args = %{pop()};
109 #warn "REMAINDER? (@_)", scalar @_;
110 0 push(@_, $args{procedural});
111 }
112 elsif( defined($_[1]) ){
113 2 ▶ $args{mode} = splice(@_, 1, 1) if $_[1] =~ /^:/;
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] =~ /^:/ 0 2 2
114 2 ▶ $args{subclass} = pop if exists($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
if exists $_[1] 2 0 2
115 }
116  
117 #Leave filehandle in @_ for pass by reference to allow gensym
118 5 ▶ $args{subclass} ||= 'IO::Pager::Unbuffered';
Condition100%2/2
$args{"subclass"} ||= "IO::Pager::Unbuffered"
l!l
23
MC/DC100%1/1
$args{"subclass"} ||= "IO::Pager::Unbuffered"
$args{"subclass"}
Truth table100%2/2
A
A= $args{"subclass"}
Aresult
11
00
119 5 $args{subclass} =~ s/^(?!IO::Pager::)/IO::Pager::/;
120 5 ▶ eval "require $args{subclass}" or die "Could not load $args{subclass}: $@\n";
Branch: 1 branch
BranchTrueFalseTotal
eval qq[require $args{"subclass"}] or die qq[Could not load $args{"subclass"}: $@\n] 0 5 5
121 5 my $token = $args{subclass}->new(@_);
122  
123 3 ▶ if( defined($args{mode}) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"mode"}) 0 3 3
124 0 $args{mode} =~ s/^\|-//;
125 0 $token->BINMODE($args{mode});
126 }
127 3 return $token;
128 }
129  
130  
131 sub _init{ # CLASS, [FH] ## Note reversal of order due to CLASS from new()
132 #Assign by reference if empty scalar given as filehandle
133 5 ▶ $_[1] = gensym() if !defined($_[1]);
Subroutine _init: called CC=8 SCAR=25.1
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[1] 3 2 5
134  
135 9 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
136 5 ▶ $_[1] ||= *{select()};
Condition33%1/3
$_[1] ||= *{select;}
l!l&&r!l&&!r
500
MC/DC0%0/2
$_[1] ||= *{select;}
$_[1] *{select;}
Truth table33%1/3
A ||= B
A= $_[1]B= *{select;}
ABresult
1X1
011
000
137  
138 # Are we on a TTY? STDOUT & STDERR are separately bound
139 5 ▶ if ( defined( my $FHn = fileno($_[1]) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $FHn = fileno $_[1])) 2 3 5
140 2 ▶ if ( $FHn == fileno(STDOUT) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($FHn == fileno STDOUT) 2 0 2
141 2 ▶ die '!TTY' unless -t $_[1];
Branch: 1 branch
BranchTrueFalseTotal
unless -t $_[1] 2 0 2
142 }
143 0 ▶ if ( $FHn == fileno(STDERR) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($FHn == fileno STDERR) 0 0 0
144 0 ▶ die '!TTY' unless -t $_[1];
Branch: 1 branch
BranchTrueFalseTotal
unless -t $_[1] 0 0 0
145 }
146 }
147  
148 #XXX This allows us to have multiple pseudo-STDOUT
149 #return 0 unless -t STDOUT;
150  
151 3 return ($_[0], $_[1]);
152 }
153  
154  
155 # Methods required for implementing a tied filehandle class
156  
157 sub TIEHANDLE {
158 3 ▶ my ($class, $tied_fh) = @_;
Subroutine TIEHANDLE: called CC=7 SCAR=22.0
159 3 ▶ unless ( $PAGER ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($IO::Pager::PAGER) 1 2 3
160 1 die "The PAGER environment variable is not defined, you may need to set it manually.";
161 }
162 2 my($real_fh, $child, $dupe_fh);
163 # XXX What about localized GLOBs?!
164 # if( $tied_fh =~ /\*(?:\w+::)?STD(?:OUT|ERR)$/ ){
165 # open($dupe_fh, '>&', $tied_fh) or warn "Unable to dupe $tied_fh";
166 # }
167 9 ▶ do{ no warnings; $child = CORE::open($real_fh, '|-', $PAGER) };
Subroutine BEGIN: called CC=1 SCAR=0.0
168 2 ▶ if ( $child ){
Branch: 1 branch
BranchTrueFalseTotal
if ($child) { } 1 1 2
169 1 my @oLayers = PerlIO::get_layers($tied_fh, details=>1, output=>1);
170 1 my $layers = '';
171 1 ▶ for(my $i=0;$i<$#oLayers;$i+=3){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $#oLayers) 0 1 1
172 #An extra base layer requires more keystrokes to exit
173 0 ▶ next if $oLayers[$i] =~ /unix|stdio/ && !defined($oLayers[+1]);
Branch: 1 branch
BranchTrueFalseTotal
if $oLayers[$i] =~ /unix|stdio/ and not defined $oLayers[1] 0 0 0
Condition0%0/3
$oLayers[$i] =~ /unix|stdio/ and not defined $oLayers[1]
!ll&&!rl&&r
000
MC/DC0%0/2
$oLayers[$i] =~ /unix|stdio/ and not defined $oLayers[1]
$oLayers[$i] =~ /unix|stdio/ not defined $oLayers[1]
Truth table0%0/3
A and not(B)
A= $oLayers[$i] =~ /unix|stdio/B= not defined $oLayers[1]
ABresult
0X0
100
111
174  
175 0 $layers .= ":$oLayers[$i]";
176 0 ▶ $layers .= '(' . ($oLayers[$i+1]) . ')' if defined($oLayers[$i+1]);
Branch: 1 branch
BranchTrueFalseTotal
if defined $oLayers[$i + 1] 0 0 0
177 }
178 1 CORE::binmode($real_fh, $layers);
179 }
180 else{
181 1 die "Could not pipe to PAGER ('$PAGER'): $!\n";
182 }
183 1 return bless {
184 'real_fh' => $real_fh,
185 # 'dupe_fh' => $dupe_fh,
186 'tied_fh' => "$tied_fh", #Avoid self-reference leak
187 'child' => $child,
188 'pager' => $PAGER,
189 }, $class;
190 }
191  
192  
193 sub BINMODE {
194 0 ▶ my ($self, $layer) = @_;
Subroutine BINMODE: not called CC=6 SCAR=37.4
195 0 ▶ if( $layer =~ /^:LOG\((>{0,2})(.*)\)$/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($layer =~ /^:LOG\((>{0,2})(.*)\)$/) { } 0 0 0
196 0 ▶ CORE::open($self->{LOG}, $1||'>', $2||"$$.log") or die $!;
Branch: 1 branch
BranchTrueFalseTotal
CORE::open $self->{"LOG"}, $1 || ">", $2 || "${$}.log" or die $! 0 0 0
Condition0%0/4
$1 || '>'
l!l
00
$2 || "${$}.log"
l!l
00
MC/DC0%0/1
$1 || '>'
$1
MC/DC0%0/1
$2 || "${$}.log"
$2
Truth table0%0/2
A
A= $1
Aresult
11
00
Truth table0%0/2
A
A= $2
Aresult
11
00
197 }
198 else{
199 0 ▶ CORE::binmode($self->{real_fh}, $layer||':raw');
Condition0%0/2
$layer || ':raw'
l!l
00
MC/DC0%0/1
$layer || ':raw'
$layer
Truth table0%0/2
A
A= $layer
Aresult
11
00
200 }
201 }
202  
203 sub WNOHANG();
204 sub EOF {
205 0 ▶ my $self = shift;
Subroutine EOF: not called CC=2 SCAR=17.9
206  
207 0 ▶ unless( defined($SIGPIPE) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $SIGPIPE) 0 0 0
208 0 eval 'use POSIX ":sys_wait_h";';
209 0 $SIGPIPE = 0;
210 }
211  
212 0 ▶ $SIG{PIPE} = sub { $SIGPIPE = 1 unless $ENV{IP_EOF};
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"IP_EOF"} 0 0 0
213 0 CORE::close($self->{real_fh});
214 0 waitpid($self->{child}, WNOHANG);
215 0 CORE::open($self->{real_fh}, '>&1');
216  
217 0 close($self->{LOG});
218 0 };
219 0 return $SIGPIPE;
220 }
221  
222  
223 sub PRINT {
224 1 ▶ my ($self, @args) = @_;
Subroutine PRINT: called CC=3 SCAR=12.0
225 1 ▶ CORE::print {$self->{LOG}} @args if exists($self->{LOG});
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"LOG"} 0 1 1
226 1 ▶ CORE::print {$self->{real_fh}} @args or die "Could not print to PAGER: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
print {$self->{"real_fh"};} @args or die "Could not print to PAGER: $!\n" 0 1 1
227 }
228  
229 sub PRINTF {
230 0 ▶ my ($self, $format, @args) = @_;
Subroutine PRINTF: not called CC=1 SCAR=6.9
231 0 $self->PRINT(sprintf($format, @args));
232 }
233  
234  
235 sub say {
236 0 ▶ my ($self, @args) = @_;
Subroutine say: not called CC=1 SCAR=6.9
Pod undocumented
237 0 $args[-1] .= "\n";
238 0 $self->PRINT(@args);
239 }
240  
241 sub WRITE {
242 0 ▶ my ($self, $scalar, $length, $offset) = @_;
Subroutine WRITE: not called CC=2 SCAR=17.9
243 0 ▶ $self->PRINT(substr($scalar, $offset||0, $length));
Condition0%0/2
$offset || 0
l!l
00
MC/DC0%0/1
$offset || 0
$offset
Truth table0%0/2
A
A= $offset
Aresult
11
00
244 }
245  
246  
247 sub TELL {
248 #Buffered classes provide their own, and others may use this in another way
249 0 ▶ return undef;
Subroutine TELL: not called CC=1 SCAR=6.9
250 }
251  
252  
253 sub FILENO {
254 0 ▶ CORE::fileno($_[0]->{real_fh});
Subroutine FILENO: not called CC=1 SCAR=6.9
255 }
256  
257 sub CLOSE {
258 1 ▶ my ($self) = @_;
Subroutine CLOSE: called CC=1 SCAR=0.0
259 1 CORE::close($self->{real_fh});
260 # untie($self->{tied_fh});
261 # *{$self->{tied_fh}} = *{$self->{dupe_fh}};
262 }
263  
264 9 ▶{ no warnings 'once'; *DESTROY = \&CLOSE; }
Subroutine BEGIN: called CC=1 SCAR=0.0
265  
266  
267 #Non-IO methods
268 sub PID{
269 2 ▶ my ($self) = @_;
Subroutine PID: called CC=1 SCAR=0.0
Pod documented
270 2 return $self->{child};
271 }
272  
273  
274 #Provide lowercase aliases for accessors
275 9 foreach my $method ( qw(BINMODE CLOSE EOF PRINT PRINTF TELL WRITE PID) ){
276 9 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
277 72 *{lc($method)} = \&{$method};
278 }
279  
280  
281 9 1;
282  
283 __END__
« Pager/Perl.pm ↑ Summary Pager/Buffered.pm »