blib/lib/goto/file.pm

filter: Statementstmt 100% 53 / 53 Branchbran 93.7% 15 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod 0.0% 0 / 1 totaltotal 97.3% 73 / 75 CC 9 SCAR 14.4
CC
9
Cov
99%
filter
6.0
import
4.0
BEGIN
1.0
CRAP 4.2
↑ Summary
1 package goto::file;
2 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 6 our $VERSION = '0.005';
6  
7 7 ▶use Filter::Util::Call qw/filter_add filter_read/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 our %HANDLES;
10  
11 my $ID = 1;
12 7 ▶sub import {
Subroutine import: called CC=4 SCAR=13.9
13 5 my $class = shift;
14 5 my ($in) = @_;
15  
16 5 ▶ return unless $in;
Branch: 1 branch
BranchTrueFalseTotal
unless $in 2 5 7
17  
18 1 my ($pkg, $file, $line) = caller(0);
19 1 my ($fh, @lines);
20  
21 1 ▶ if (ref($in) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $in eq 'ARRAY') { } 1 4 5
22 1 my $safe = $file;
23 4 $safe =~ s/"/\\"/;
24  
25 4 push @lines => "#line " . (__LINE__ + 1) . ' "' . __FILE__ . '"';
26 4 push @lines => (
27 "package main;",
28 "#line 1 \"lines from $safe line $line\"",
29 @$in,
30 );
31 }
32 else {
33 4 push @lines => "#line " . (__LINE__ + 1) . ' "' . __FILE__ . '"';
34 4 push @lines => "package main;";
35 4 push @lines => "\$@ = '';";
36  
37 4 my $id = $ID++;
38  
39 4 ▶ open($fh, '<', $in) or die "Cold not open file '$in': $!";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $in or die "Cold not open file '${in}': $!" 4 0 4
40  
41 4 $HANDLES{$id} = $fh;
42 4 my $safe = $in;
43 5 $safe =~ s/"/\\"/;
44 86 push @lines => "#line " . (__LINE__ + 2) . ' "' . __FILE__ . '"';
45 86 push @lines => (
46 10 '{ local ($!, $?, $^E, $@); close(DATA); *DATA = $' . __PACKAGE__ . '::HANDLES{' . $id . '} }',
47 qq{#line 1 "$safe"},
48 );
49 }
50  
51 5 Filter::Util::Call::filter_add(
52 bless {fh => $fh, lines => \@lines, file => $in, caller => [$pkg, $file, $line]},
53 $class
54 );
55 }
56  
57 86 ▶sub filter {
Subroutine filter: called CC=6 SCAR=17.9
Pod undocumented
58 5 my $self = shift;
59  
60 23 ▶ unless ($self->{init}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"init"}) 5 81 86
61 5 $self->{init} = 1;
62 86 ▶ while (1) { filter_read() or last }
Branch: 1 branch
BranchTrueFalseTotal
&filter_read() or last 18 5 23
63 86 $_ = '';
64 }
65  
66 86 my $lines = $self->{lines};
67 30 my $fh = $self->{fh};
68  
69 30 my $line;
70 55 ▶ if (@$lines) {
Branch: 2 branches
BranchTrueFalseTotal
if (@$lines) { } 30 56 86
elsif ($fh) { } 55 1 56
71 55 chomp($line = shift @$lines);
72 86 $line .= "\n";
73 }
74 elsif($fh) {
75 # We do this to prevent ', <$fh> at line #' being appended to
76 # exceptions and warnings.
77 83 local $.;
78  
79 83 $line = <$fh>;
80 }
81  
82 3 ▶ if (defined $line) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line) 83 3 86
83 6 $_ .= $line;
84 6 return 1;
85 }
86  
87 6 return 0;
88 }
89  
90 1;
91  
92 __END__
↑ Summary