| 1 | package Data::Embed::File; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # ABSTRACT: embed arbitrary data in a file | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 9 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 9 | ▶ | use English qw< -no_match_vars >; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 9 | ▶ | use IO::Slice; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 9 | ▶ | use Fcntl qw< :seek >; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 9 | ▶ | use Log::Log4perl::Tiny qw< :easy >; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 9 | ▶ | use Scalar::Util qw< refaddr blessed >; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 9 | our $VERSION = '0.32'; # make indexer happy | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 56 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=8 SCAR=26.4
Pod
documented
| |||||||||||||||||||||||||||||
| 16 | 56 | my $package = shift; | |||||||||||||||||||||||||||
| 17 | 0 | ▶ | my $self = {(scalar(@_) && ref($_[0])) ? %{$_[0]} : @_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
scalar @_ and ref $_[0]
MC/DC0%0/2
scalar @_ and ref $_[0]
scalar @_
ref $_[0]
Truth table33%1/3
A and B
A= scalar @_B= ref $_[0]
| |||||||||||||||||||||||||||||
| 18 | 56 | for my $feature (qw< offset length >) { | |||||||||||||||||||||||||||
| 19 | LOGCROAK "$package new(): missing required field $feature" | ||||||||||||||||||||||||||||
| 20 | unless defined($self->{$feature}) | ||||||||||||||||||||||||||||
| 21 | 112 | ▶ | && $self->{$feature} =~ m{\A\d+\z}mxs; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $self->{$feature} and $self->{$feature} =~ /\A\d+\z/msx
MC/DC0%0/2
defined $self->{$feature} and $self->{$feature} =~ /\A\d+\z/msx
defined $self->{$feature}
$self->{$feature} =~ /\A\d+\z/msx
Truth table33%1/3
A and B
A= defined $self->{$feature}B= $self->{$feature} =~ /\A\d+\z/msx
| |||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | LOGDIE "$package new(): either filename or fh are required" | ||||||||||||||||||||||||||||
| 24 | 56 | ▶ | unless defined($self->{fh}) || defined($self->{filename}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $self->{"fh"} or defined $self->{"filename"}
MC/DC0%0/2
defined $self->{"fh"} or defined $self->{"filename"}
defined $self->{"fh"}
defined $self->{"filename"}
Truth table33%1/3
A or B
A= defined $self->{"fh"}B= defined $self->{"filename"}
| |||||||||||||||||||||||||||||
| 25 | 56 | return bless $self, $package; | |||||||||||||||||||||||||||
| 26 | } ## end sub new | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 21 | ▶ | sub fh { | ||||||||||||||||||||||||||
|
Subroutine
fh: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 29 | 21 | my $self = shift; | |||||||||||||||||||||||||||
| 30 | 21 | ▶ | if (!exists $self->{slicefh}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 84 | my %args = map { $_ => $self->{$_} } | |||||||||||||||||||||||||||
| 32 | 21 | grep { defined $self->{$_} } qw< fh filename offset length >; | |||||||||||||||||||||||||||
| 33 | 21 | $self->{slicefh} = IO::Slice->new(%args); | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | 21 | return $self->{slicefh}; | |||||||||||||||||||||||||||
| 36 | } ## end sub fh | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 15 | ▶ | sub contents { | ||||||||||||||||||||||||||
|
Subroutine
contents: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 39 | 15 | my $self = shift; | |||||||||||||||||||||||||||
| 40 | 15 | my $fh = $self->fh(); | |||||||||||||||||||||||||||
| 41 | 15 | my $current = tell $fh; | |||||||||||||||||||||||||||
| 42 | 15 | seek $fh, 0, SEEK_SET; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 15 | ▶ | local $/ = wantarray() ? $/ : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 15 | my @retval = <$fh>; | |||||||||||||||||||||||||||
| 46 | 15 | seek $fh, $current, SEEK_SET; | |||||||||||||||||||||||||||
| 47 | 15 | ▶ | return @retval if wantarray(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 5 | return $retval[0]; | |||||||||||||||||||||||||||
| 49 | } ## end sub contents | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 5 | ▶ | sub name { return shift->{name}; } | ||||||||||||||||||||||||||
|
Subroutine
name: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 4 | ▶ | sub _dname { | ||||||||||||||||||||||||||
|
Subroutine
_dname: called CC=2 SCAR=8.1
| |||||||||||||||||||||||||||||
| 54 | 4 | my $name = shift->{name}; | |||||||||||||||||||||||||||
| 55 | 4 | ▶ | return $name if defined $name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 0 | return ''; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 4 | ▶ | sub is_same_as { | ||||||||||||||||||||||||||
|
Subroutine
is_same_as: called CC=12 SCAR=35.0
Pod
documented
| |||||||||||||||||||||||||||||
| 60 | 4 | my ($self, $other) = @_; | |||||||||||||||||||||||||||
| 61 | 4 | ▶ | return unless blessed($other); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 3 | ▶ | return unless $other->isa('Data::Embed::File'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | # quick wins | ||||||||||||||||||||||||||||
| 65 | 3 | ▶ | return unless $self->{offset} == $other->{offset}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 2 | ▶ | return unless $self->{length} == $other->{length}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | # names must be the same | ||||||||||||||||||||||||||||
| 69 | 2 | ▶ | return unless $self->_dname() eq $other->_dname(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | # check data sources | ||||||||||||||||||||||||||||
| 72 | 2 | ▶ | if (defined $self->{fh}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 73 | 2 | ▶ | return unless defined $other->{fh}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 2 | return refaddr($self->{fh}) eq refaddr($other->{fh}); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | elsif (defined $self->{filename}) { | ||||||||||||||||||||||||||||
| 77 | 0 | ▶ | return unless defined $other->{filename}; # paranoid... | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 0 | ▶ | if (ref $self->{filename}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 0 | ▶ | return unless ref $other->{filename}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | 9 | 1; | |||||||||||||||||||||||||||