File Coverage

blib/lib/WARC/Record/Payload.pm
Criterion Covered Total %
statement 55 56 98.2
branch 8 10 90.0
condition n/a
subroutine 17 17 100.0
pod n/a
total 80 83 97.5


line stmt bran cond sub pod time code
1             package WARC::Record::Payload; # -*- CPerl -*-
2              
3 1     1   62080 use strict;
  1         10  
  1         27  
4 1     1   4 use warnings;
  1         1  
  1         24  
5              
6 1     1   355 use WARC; *WARC::Record::Payload::VERSION = \$WARC::VERSION;
  1         3  
  1         35  
7              
8 1     1   4 use Carp;
  1         2  
  1         43  
9 1     1   5 use Fcntl qw/SEEK_SET SEEK_CUR SEEK_END/;
  1         1  
  1         40  
10              
11             # This implementation uses an array as the underlying object.
12              
13 1     1   4 use constant { BASE => 0, HANDLE => 1, PARENT => 2 };
  1         2  
  1         67  
14 1     1   5 use constant OBJECT_INIT => undef, undef, undef;
  1         2  
  1         432  
15              
16             sub _dbg_dump {
17 8     8   21 my $self = shift;
18              
19 8         25 my $out = 'record payload @'.(tell $self->[HANDLE]);
20 8         18 $out .= "\n";
21              
22 8         25 return $out;
23             }
24              
25             sub TIEHANDLE {
26 8     8   13 my $class = shift;
27 8         10 my $parent = shift;
28 8         11 my $replay = shift;
29              
30             # The payload_offset key will be set in $parent iff loading was deferred.
31 8         13 my $handle; my $base_offset;
32 8 100       39 if (defined $parent->{payload_offset}) {
    50          
33 4         9 $handle = $parent->open_continued;
34 4         10 $base_offset = $parent->{payload_offset};
35             } elsif ($replay->can('content_ref')) {
36             # The payload was smaller than the deferred loading threshold.
37             # This open fails iff perl was built without PerlIO, which is non-default.
38             # uncoverable branch true
39 1 50   1   38 open $handle, '<', $replay->content_ref
  1         1  
  1         6  
  4         17  
40             or die "failure opening stream on replay object buffer: $!";
41 4         728 binmode $handle, ':raw';
42 4         7 $base_offset = 0;
43             } else
44 0         0 { confess 'extracting payload from '.(ref $replay).' not implemented' }
45              
46 8         17 my $ob = [OBJECT_INIT];
47 8         18 @$ob[PARENT, HANDLE, BASE] = ($parent, $handle, $base_offset);
48              
49 8         20 bless $ob, $class;
50              
51 8         21 $ob->SEEK(0, SEEK_SET);
52              
53 8         26 return $ob;
54             }
55              
56             # TODO: implement payload decoding
57              
58 12     12   65 sub READLINE { readline $_[0]->[HANDLE] }
59              
60             # This sub must rely on the aliasing effect of @_.
61 4 100   4   23 sub READ { read $_[0]->[HANDLE], $_[1], $_[2], (defined $_[3] ? $_[3] : 0) }
62              
63 18     18   88 sub GETC { getc $_[0]->[HANDLE] }
64              
65 8     8   56 sub EOF { eof $_[0]->[HANDLE] }
66              
67             sub SEEK {
68 12     12   33 my $self = shift;
69 12         13 my $offset = shift;
70 12         14 my $whence = shift;
71              
72 12         14 my $adjust = 0;
73 12 100       29 $adjust = $self->[BASE] if $whence == SEEK_SET;
74              
75 12         39 seek $self->[HANDLE], $adjust + $offset, $whence
76             }
77              
78 4     4   19 sub TELL { return ((tell $_[0]->[HANDLE]) - $_[0]->[BASE]) }
79              
80 2     2   10 sub CLOSE { close $_[0]->[HANDLE] }
81              
82             1;
83             __END__