| 1 | package Sys::Export::LazyFileData; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 12 | our $VERSION = '0.006'; # VERSION | |||||||||||||||||||||||||||||||||||||||||
| 4 | # ABSTRACT: Reference a path and optional range of bytes and load it on demand | ||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 12 | ▶ | use v5.26; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 12 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 12 | ▶ | use experimental qw( signatures ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 12 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 12 | ▶ | use overload q{""} => \&as_string, q{${}} => \&as_scalarref; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 12 | ▶ | use Cwd (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 12 | ▶ | use Sys::Export (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 32 | ▶ | sub new($class, $src, $offset=0, $size=undef) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=12 SCAR=26.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 32 | my ($abs, $actual_size); | |||||||||||||||||||||||||||||||||||||||||
| 17 | 32 | ▶ | if (ref $src eq 'SCALAR') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | # Ensure is only bytes | ||||||||||||||||||||||||||||||||||||||||||
| 19 | 3 | ▶ | croak "Wide character in file data scalar ref" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
utf8::is_utf8($src) and not utf8::downgrade($src, 1)
MC/DC0%0/2
utf8::is_utf8($src) and not utf8::downgrade($src, 1)
utf8::is_utf8($src)
not utf8::downgrade($src, 1)
Truth table33%1/3
A and not(B)
A= utf8::is_utf8($src)B= not utf8::downgrade($src, 1)
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | if utf8::is_utf8($src) && !utf8::downgrade($src, 1); | ||||||||||||||||||||||||||||||||||||||||||
| 21 | # If given a scalar-ref, and size and offset are the defaults, then we can just cache that directly. | ||||||||||||||||||||||||||||||||||||||||||
| 22 | 3 | ▶ | return bless [ $src, $src, 0, length $$src ], $class | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
not $offset and !$size || $size == length $$src
not $size or $size == length $$src
MC/DC33%1/3
not $offset and !$size || $size == length $$src
not $offset
not $size
$size == length $$src
Truth table50%2/4
not(A) and not(B) or C
A= not $offsetB= not $sizeC= $size == length $$src
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | if !$offset && (!$size || $size == length $$src); | ||||||||||||||||||||||||||||||||||||||||||
| 24 | 2 | $actual_size= length $$src; | |||||||||||||||||||||||||||||||||||||||||
| 25 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 26 | 29 | ▶ | $abs= Cwd::abs_path($src) // croak "Can't resolve '$src' to a real file"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
Cwd::abs_path($src) // croak("Can't resolve '${src}' to a real file")
MC/DC0%0/1
Cwd::abs_path($src) // croak("Can't resolve '${src}' to a real file")
Cwd::abs_path($src)
Truth table50%1/2
A
A= Cwd::abs_path($src)
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | 29 | $actual_size= -s $abs; | |||||||||||||||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||||||||||||||
| 29 | 31 | ▶ | $size //= $actual_size - $offset; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$size //= $actual_size - $offset
MC/DC100%1/1
$size //= $actual_size - $offset
$size
Truth table100%2/2
A
A= $size
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | 31 | ▶ | croak "Requested size ($src, $offset+$size) exceeds actual size ($actual_size)" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | if $offset+$size > $actual_size; | ||||||||||||||||||||||||||||||||||||||||||
| 32 | 31 | return bless [ undef, $src, $offset, $size, $abs ], $class; | |||||||||||||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | ▶ | sub source { $_[0][1] } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
source: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | 6 | ▶ | sub offset { $_[0][2] } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
offset: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | 7 | ▶ | sub size { $_[0][3] } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
size: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 2 | ▶ | sub abs_path { $_[0][4] } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
abs_path: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | 43 | ▶ | sub as_scalarref { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
as_scalarref: called CC=3 SCAR=13.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | ▶ | $_[0][0] //= ref $_[0][1] eq 'SCALAR'? \substr(${$_[0][1]}, $_[0][2], $_[0][3]) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$_[0][0] //= ref $_[0][1] eq "SCALAR" ? \substr(${$_[0][1];}, $_[0][2], $_[0][3]) : Sys::Export::map_or_load_file($_[0][1], $_[0][2], $_[0][3])
MC/DC0%0/2
$_[0][0] //= ref $_[0][1] eq "SCALAR" ? \substr(${$_[0][1];}, $_[0][2], $_[0][3]) : Sys::Export::map_or_load_file($_[0][1], $_[0][2], $_[0][3])
$_[0][0]
ref $_[0][1] eq "SCALAR" ? \substr(${$_[0][1];}, $_[0][2], $_[0][3]) : Sys::Export::map_or_load_file($_[0][1], $_[0][2], $_[0][3])
Truth table66%2/3
A //= B
A= $_[0][0]B= ref $_[0][1] eq "SCALAR" ? \substr(${$_[0][1];}, $_[0][2], $_[0][3]) : Sys::Export::map_or_load_file($_[0][1], $_[0][2], $_[0][3])
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | : Sys::Export::map_or_load_file($_[0][1], $_[0][2], $_[0][3]); | ||||||||||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | ▶ | sub as_string { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
as_string: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | ${ $_[0][0] // $_[0]->as_scalarref } | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$_[0][0] // $_[0]->as_scalarref
MC/DC0%0/2
$_[0][0] // $_[0]->as_scalarref
$_[0][0]
$_[0]->as_scalarref
Truth table0%0/3
A // B
A= $_[0][0]B= $_[0]->as_scalarref
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | # Avoiding dependency on namespace::clean | ||||||||||||||||||||||||||||||||||||||||||
| 51 | 12 | delete @{Sys::Export::LazyFileData::}{qw( carp croak confess )}; | |||||||||||||||||||||||||||||||||||||||||
| 52 | 12 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | __END__ | ||||||||||||||||||||||||||||||||||||||||||