| 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
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 65 | ▶ | opendir my $dir, $dirname or die "autoload dirscan $dirname: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 68 | ▶ | open my $file, '<', $filename or die "autoload scan $filename: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 68 | my $descriptor_found = 0; | |||||||||||||||||||||||||||
| 73 | LINE: | ||||||||||||||||||||||||||||
| 74 | 68 | ▶ | while (<$file>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 768 | ▶ | if (m/^=(?:for|begin)\s+autoload(?:\s+|$)/) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 68 | { $descriptor_found = 1; next LINE; } | |||||||||||||||||||||||||||
| 77 | 700 | ▶ | next LINE unless $descriptor_found; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 266 | ▶ | last LINE if m/^=/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 208 | ▶ | if (m/^\[WARC::Record::Replay\]$/) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 68 | { $descriptor_found = 2; next LINE; } | |||||||||||||||||||||||||||
| 82 | 140 | ▶ | next LINE if $descriptor_found < 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 130 | ▶ | last LINE if m/^\[/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | # ... parse and test conditional; load if matched | ||||||||||||||||||||||||||||
| 86 | 126 | ▶ | if (m/^([[:alpha:]][_[:alnum:]]*)\(([-_[:alnum:]]*)\)\s*=\s*(.+)$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
$match_valid and $match_value =~ /$3/
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/
| |||||||||||||||||||||||||||||
| 91 | 6 | { require $modfilename; $loaded++; last LINE } | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | 67 | ▶ | close $file or die "autoload scan close $file: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
ref $_[0] eq "CODE" and ref $_[1] eq "CODE"
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"
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 159 | ▶ | if (scalar @handlers == 0 and _try_autoload_for $record) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
scalar @handlers == 0 and &_try_autoload_for($record)
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)
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||