WARC/Record/Replay.pm

filter: Statementstmt 100% 76 / 76 Branchbran 86.1% 31 / 36 Conditioncond 100% 9 / 9 MC/DCmcdc 100% 6 / 6 Subroutinesub 100% 8 / 8 Podpod 100% 2 / 2 totaltotal 96.3% 132 / 137 CC 25 SCAR 25.3
CC
25
Cov
96%
_try_autoload_for
19.1
find_handlers
5.0
register
3.0
CRAP 12.6
« WARC/Record/Logical/Heuristics.pm ↑ Summary WARC/Collection.pm »
1 package WARC::Record::Replay; # -*- CPerl -*-
2  
3 28 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 28 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 28 ▶use WARC; *WARC::Record::Replay::VERSION = \$WARC::VERSION;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 WARC::Record::Replay - WARC record replay registry and autoloading
11  
12 =head1 SYNOPSIS
13  
14 use WARC::Record;
15  
16 $object = $record->replay;
17  
18 =cut
19  
20 28 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 28 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 # array of arrays:
24 # ( [ predicate coderef, handler coderef ]... )
25 28 our @Handlers = ();
26 # Each predicate is called with the record object locally stored in $_ and
27 # must return false to reject the record or true to accept the record.
28 #
29 # Each handler for which the predicate returns true is tried in order.
30  
31 =head1 DESCRIPTION
32  
33 This is an internal module that provides a registry of protocol replay
34 support modules and an autoloading facility.
35  
36 =over
37  
38 =cut
39  
40 # Scan @INC for autoload descriptors and see if any available modules can
41 # be autoloaded for this record.
42  
43 sub _try_autoload_for ($) {
44 25 ▶ my $record = shift;
Subroutine _try_autoload_for: called CC=19 SCAR=29.5
45 25 my $loaded = 0;
46  
47 25 local *_;
48 25 foreach my $area (@INC) {
49 169 my $vol; my $dirpath; my $tail;
50 169 ($vol, $dirpath, $tail) = File::Spec->splitpath($area);
51 169 my @dirs = File::Spec->splitdir($dirpath);
52 169 my $dirname = File::Spec->catpath
53 ($vol, File::Spec->catdir(@dirs, $tail, qw/WARC Record Replay/));
54  
55 169 ▶ next unless -d $dirname;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dirname 104 65 169
56  
57 65 ▶ opendir my $dir, $dirname or die "autoload dirscan $dirname: $!";
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dir, $dirname or die "autoload dirscan ${dirname}: $!" 0 65 65
58 65 my @modules = grep defined, map {/^([[:alnum:]_]+[.]pm)$/; $1} # untaint
59 grep /[.]pm$/, readdir $dir;
60 65 ▶ closedir $dir or die "autoload dirscan close $dirname: $!";
Branch: 1 branch
BranchTrueFalseTotal
closedir $dir or die "autoload dirscan close ${dirname}: $!" 0 65 65
61  
62 FILE:
63 65 foreach my $module (@modules) {
64 85 my $filename = File::Spec->catpath
65 ($vol, File::Spec->catdir(@dirs, $tail, qw/WARC Record Replay/),
66 $module);
67 85 my $modfilename = File::Spec::Unix->catfile
68 (qw/WARC Record Replay/, $module);
69 85 ▶ next FILE if $INC{$modfilename};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{$modfilename} 17 68 85
70  
71 68 ▶ open my $file, '<', $filename or die "autoload scan $filename: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $file, "<", $filename or die "autoload scan ${filename}: $!" 0 68 68
72 68 my $descriptor_found = 0;
73 LINE:
74 68 ▶ while (<$file>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $file)) 768 0 768
75 768 ▶ if (m/^=(?:for|begin)\s+autoload(?:\s+|$)/)
Branch: 1 branch
BranchTrueFalseTotal
if (/^=(?:for|begin)\s+autoload(?:\s+|$)/) 68 700 768
76 68 { $descriptor_found = 1; next LINE; }
77 700 ▶ next LINE unless $descriptor_found;
Branch: 1 branch
BranchTrueFalseTotal
unless $descriptor_found 434 266 700
78 266 ▶ last LINE if m/^=/;
Branch: 1 branch
BranchTrueFalseTotal
if /^=/ 58 208 266
79  
80 208 ▶ if (m/^\[WARC::Record::Replay\]$/)
Branch: 1 branch
BranchTrueFalseTotal
if (/^\[WARC::Record::Replay\]$/) 68 140 208
81 68 { $descriptor_found = 2; next LINE; }
82 140 ▶ next LINE if $descriptor_found < 2;
Branch: 1 branch
BranchTrueFalseTotal
if $descriptor_found < 2 10 130 140
83 130 ▶ last LINE if m/^\[/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\[/ 4 126 130
84  
85 # ... parse and test conditional; load if matched
86 126 ▶ if (m/^([[:alpha:]][_[:alnum:]]*)\(([-_[:alnum:]]*)\)\s*=\s*(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^([[:alpha:]][_[:alnum:]]*)\(([-_[:alnum:]]*)\)\s*=\s*(.+)$/) 68 58 126
87 # $1: method $2: argument $3: match
88 68 my $match_valid = 0; my $match_value;
89 68 eval {$match_value = $record->$1($2); $match_valid = 1};
90 68 ▶ if ($match_valid and $match_value =~ $3)
Branch: 1 branch
BranchTrueFalseTotal
if ($match_valid and $match_value =~ /$3/) 6 62 68
Condition100%3/3
$match_valid and $match_value =~ /$3/
!ll&&!rl&&r
5576
MC/DC100%2/2
$match_valid and $match_value =~ /$3/
$match_valid $match_value =~ /$3/
Truth table100%3/3
A and B
A= $match_validB= $match_value =~ /$3/
ABresult
0X0
100
111
91 6 { require $modfilename; $loaded++; last LINE }
92 }
93 }
94 67 ▶ close $file or die "autoload scan close $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $file or die "autoload scan close ${file}: $!" 0 67 67
95 }
96 }
97  
98 24 return $loaded
99 }
100  
101 =item WARC::Record::Replay::register { predicate } $handler
102  
103 Add a handler to the internal list of replay handlers. The given handler
104 will be used for records on which the given predicate returns true.
105  
106 The predicate will be evaluated with $_ locally set to the record object to
107 be replayed and @_ empty each time a record is replayed.
108  
109 =cut
110  
111 sub register (&$) {
112 25 ▶ croak "attempt to register invalid handler"
Subroutine register: called CC=3 SCAR=11.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "CODE" and ref $_[1] eq "CODE" 2 23 25
Condition100%3/3
ref $_[0] eq "CODE" and ref $_[1] eq "CODE"
!ll&&!rl&&r
1123
MC/DC100%2/2
ref $_[0] eq "CODE" and ref $_[1] eq "CODE"
ref $_[0] eq "CODE" ref $_[1] eq "CODE"
Truth table100%3/3
A and B
A= ref $_[0] eq "CODE"B= ref $_[1] eq "CODE"
ABresult
0X0
100
111
113 unless (ref $_[0] eq 'CODE') && (ref $_[1] eq 'CODE');
114  
115 23 push @Handlers, [ @_[0, 1] ];
116  
117 return # nothing
118 23 }
119  
120 =item WARC::Record::Replay::find_handlers( $record )
121  
122 Return a list of handlers that can replay the protocol message in $record.
123  
124 =cut
125  
126 sub find_handlers ($) {
127 159 ▶ my $record = shift;
Subroutine find_handlers: called CC=5 SCAR=16.1
Pod documented
128 159 my @handlers = ();
129  
130 {
131 159 local *_; $_ = $record;
132 159 foreach my $handler (@Handlers)
133 538 ▶ { push @handlers, $handler->[1] if $handler->[0]->() }
Branch: 1 branch
BranchTrueFalseTotal
if $handler->[0]->() 235 303 538
134 }
135  
136 159 ▶ if (scalar @handlers == 0 and _try_autoload_for $record)
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @handlers == 0 and &_try_autoload_for($record)) 5 153 158
Condition100%3/3
scalar @handlers == 0 and &_try_autoload_for($record)
!ll&&!rl&&r
134196
MC/DC100%2/2
scalar @handlers == 0 and &_try_autoload_for($record)
scalar @handlers == 0 &_try_autoload_for($record)
Truth table100%3/3
A and B
A= scalar @handlers == 0B= &_try_autoload_for($record)
ABresult
0X0
100
111
137 # repeat the search now that a module has been loaded
138 5 { unshift @_, $record; goto &find_handlers }
139  
140 return @handlers
141 153 }
142  
143 =back
144  
145 =cut
146  
147 28 1;
148 __END__
« WARC/Record/Logical/Heuristics.pm ↑ Summary WARC/Collection.pm »