File Coverage

blib/lib/Data/Embed/File.pm
Criterion Covered Total %
statement 55 64 85.9
branch 18 36 50.0
condition 3 9 33.3
subroutine 13 13 100.0
pod 5 5 100.0
total 94 127 74.0


line stmt bran cond sub pod time code
1             package Data::Embed::File;
2              
3             # ABSTRACT: embed arbitrary data in a file
4              
5 9     9   32 use strict;
  9         11  
  9         216  
6 9     9   27 use warnings;
  9         90  
  9         263  
7 9     9   48 use English qw< -no_match_vars >;
  9         9  
  9         43  
8 9     9   6302 use IO::Slice;
  9         24411  
  9         234  
9 9     9   38 use Fcntl qw< :seek >;
  9         7  
  9         762  
10 9     9   34 use Log::Log4perl::Tiny qw< :easy >;
  9         10  
  9         28  
11 9     9   1815 use Scalar::Util qw< refaddr blessed >;
  9         9  
  9         3984  
12              
13             our $VERSION = '0.32'; # make indexer happy
14              
15             sub new {
16 56     56 1 41 my $package = shift;
17 56 50 33     273 my $self = {(scalar(@_) && ref($_[0])) ? %{$_[0]} : @_};
  0         0  
18 56         58 for my $feature (qw< offset length >) {
19             LOGCROAK "$package new(): missing required field $feature"
20             unless defined($self->{$feature})
21 112 50 33     490 && $self->{$feature} =~ m{\A\d+\z}mxs;
22             }
23             LOGDIE "$package new(): either filename or fh are required"
24 56 50 33     90 unless defined($self->{fh}) || defined($self->{filename});
25 56         191 return bless $self, $package;
26             } ## end sub new
27              
28             sub fh {
29 21     21 1 1249 my $self = shift;
30 21 50       45 if (!exists $self->{slicefh}) {
31 84         143 my %args = map { $_ => $self->{$_} }
32 21         24 grep { defined $self->{$_} } qw< fh filename offset length >;
  84         104  
33 21         79 $self->{slicefh} = IO::Slice->new(%args);
34             }
35 21         842 return $self->{slicefh};
36             } ## end sub fh
37              
38             sub contents {
39 15     15 1 1320 my $self = shift;
40 15         20 my $fh = $self->fh();
41 15         39 my $current = tell $fh;
42 15         46 seek $fh, 0, SEEK_SET;
43              
44 15 100       238 local $/ = wantarray() ? $/ : undef;
45 15         30 my @retval = <$fh>;
46 15         393 seek $fh, $current, SEEK_SET;
47 15 100       185 return @retval if wantarray();
48 5         16 return $retval[0];
49             } ## end sub contents
50              
51 5     5 1 9 sub name { return shift->{name}; }
52              
53             sub _dname {
54 4     4   5 my $name = shift->{name};
55 4 50       9 return $name if defined $name;
56 0         0 return '';
57             }
58              
59             sub is_same_as {
60 4     4 1 2 my ($self, $other) = @_;
61 4 100       16 return unless blessed($other);
62 3 50       11 return unless $other->isa('Data::Embed::File');
63              
64             # quick wins
65 3 100       7 return unless $self->{offset} == $other->{offset};
66 2 50       2 return unless $self->{length} == $other->{length};
67              
68             # names must be the same
69 2 50       3 return unless $self->_dname() eq $other->_dname();
70              
71             # check data sources
72 2 50       4 if (defined $self->{fh}) {
    0          
73 2 50       3 return unless defined $other->{fh};
74 2         9 return refaddr($self->{fh}) eq refaddr($other->{fh});
75             }
76             elsif (defined $self->{filename}) {
77 0 0         return unless defined $other->{filename}; # paranoid...
78 0 0         if (ref $self->{filename}) {
79 0 0         return unless ref $other->{filename};
80 0           return refaddr($self->{filename}) eq refaddr($other->{filename});
81             }
82 0           return $self->{filename} eq $other->{filename};
83             } ## end elsif (defined $self->{filename...})
84             else { # paranoid!
85 0           return;
86             }
87              
88 0           return 1; # you made it!
89             } ## end sub is_same_as
90              
91             1;