| 1 | 10 | ▶ | use 5.008008; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 2 | 10 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 10 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | package Data::Smile::XS; | ||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 10 | our $AUTHORITY = 'cpan:TOBYINK'; | |||||||||||||||||||||||||||||||||||||||||
| 8 | 10 | our $VERSION = '0.001000'; | |||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 10 | require XSLoader; | |||||||||||||||||||||||||||||||||||||||||
| 11 | 10 | XSLoader::load( __PACKAGE__, $VERSION ); | |||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 10 | ▶ | use Exporter::Tiny; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 10 | our @ISA = qw( Exporter::Tiny ); | |||||||||||||||||||||||||||||||||||||||||
| 15 | 10 | our @EXPORT_OK = qw( encode_smile decode_smile dump_smile load_smile ); | |||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | 24 | ▶ | sub _is_pathish_filename { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_is_pathish_filename: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 24 | my ( $x ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 19 | 24 | ▶ | return 0 if not ref $x; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 19 | ▶ | return 1 if eval { $x->isa( 'Path::Tiny' ) } or eval { $x->isa( 'Path::Class::File' ) }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
eval {
do {
$x->isa("Path::Tiny")
}
} or eval {
do {
$x->isa("Path::Class::File")
}
}
MC/DC50%1/2
eval {
do {
$x->isa("Path::Tiny")
}
} or eval {
do {
$x->isa("Path::Class::File")
}
}
eval {
do {
$x->isa("Path::Tiny")
}
}
eval {
do {
$x->isa("Path::Class::File")
}
}
Truth table66%2/3
A or B
A= eval {
do {
$x->isa("Path::Tiny")
}
}B= eval {
do {
$x->isa("Path::Class::File")
}
}
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 1 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 19 | ▶ | sub _open_for_read { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_open_for_read: called CC=7 SCAR=29.1
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | 19 | my ( $file ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | 19 | ▶ | if ( _is_pathish_filename( $file ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 17 | $file = "$file"; | |||||||||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | 19 | ▶ | if ( not ref $file ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 19 | ▶ | open my $fh, '<:raw', $file or die "open($file) for read: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 19 | return ( $fh, 1 ); | |||||||||||||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | ▶ | if ( eval { $file->can( 'read' ) } or eval { $file->can( 'getline' ) } or ref( $file ) eq 'GLOB' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
eval {
do {
$file->can("read")
}
} or eval {
do {
$file->can("getline")
}
} or ref $file eq "GLOB"
eval {
do {
$file->can("read")
}
} or eval {
do {
$file->can("getline")
}
}
MC/DC0%0/3
eval {
do {
$file->can("read")
}
} or eval {
do {
$file->can("getline")
}
} or ref $file eq "GLOB"
eval {
do {
$file->can("read")
}
}
eval {
do {
$file->can("getline")
}
}
ref $file eq "GLOB"
Truth table0%0/4
A or B or C
A= eval {
do {
$file->can("read")
}
}B= eval {
do {
$file->can("getline")
}
}C= ref $file eq "GLOB"
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | binmode( $file, ':raw' ); | |||||||||||||||||||||||||||||||||||||||||
| 38 | 0 | return ( $file, 0 ); | |||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | die "Unsupported file argument for read"; | |||||||||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | 5 | ▶ | sub _open_for_write { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_open_for_write: called CC=7 SCAR=21.3
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 5 | my ( $file ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | 5 | ▶ | if ( _is_pathish_filename( $file ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 1 | $file = "$file"; | |||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | 5 | ▶ | if ( not ref $file ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | 4 | ▶ | open my $fh, '>:raw', $file or die "open($file) for write: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | 4 | return ( $fh, 1 ); | |||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | ▶ | if ( eval { $file->can( 'print' ) } or eval { $file->can( 'write' ) } or ref( $file ) eq 'GLOB' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
eval {
do {
$file->can("print")
}
} or eval {
do {
$file->can("write")
}
} or ref $file eq "GLOB"
eval {
do {
$file->can("print")
}
} or eval {
do {
$file->can("write")
}
}
MC/DC0%0/3
eval {
do {
$file->can("print")
}
} or eval {
do {
$file->can("write")
}
} or ref $file eq "GLOB"
eval {
do {
$file->can("print")
}
}
eval {
do {
$file->can("write")
}
}
ref $file eq "GLOB"
Truth table25%1/4
A or B or C
A= eval {
do {
$file->can("print")
}
}B= eval {
do {
$file->can("write")
}
}C= ref $file eq "GLOB"
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 1 | binmode( $file, ':raw' ); | |||||||||||||||||||||||||||||||||||||||||
| 58 | 1 | return ( $file, 0 ); | |||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | die "Unsupported file argument for write"; | |||||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | 5 | ▶ | sub dump_smile { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
dump_smile: called CC=10 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 5 | my ( $file, $data, $opts ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 66 | 5 | ▶ | die 'dump_smile expects at most 3 arguments' if @_ > 3; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | 5 | ▶ | if ( @_ >= 3 and defined $opts ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@_ >= 3 and defined $opts
MC/DC50%1/2
@_ >= 3 and defined $opts
@_ >= 3
defined $opts
Truth table66%2/3
A and B
A= @_ >= 3B= defined $opts
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 2 | ▶ | die 'Options must be a hash reference' if ref( $opts ) ne 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | 5 | my ( $fh, $close ) = _open_for_write( $file ); | |||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | 5 | my $bytes = encode_smile( $data, $opts ); | |||||||||||||||||||||||||||||||||||||||||
| 75 | 4 | ▶ | my $ok = eval { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | 4 | ▶ | print {$fh} $bytes or die "write: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 4 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 78 | } ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | 4 | ▶ | if ( $close ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 3 | ▶ | close $fh or $ok = 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | 4 | ▶ | return $ok ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | 19 | ▶ | sub load_smile { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
load_smile: called CC=9 SCAR=22.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 19 | my ( $file, $opts ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 89 | 19 | ▶ | die 'load_smile expects at most 2 arguments' if @_ > 2; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | 19 | ▶ | if ( @_ >= 2 and defined $opts ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@_ >= 2 and defined $opts
MC/DC50%1/2
@_ >= 2 and defined $opts
@_ >= 2
defined $opts
Truth table66%2/3
A and B
A= @_ >= 2B= defined $opts
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | 1 | ▶ | die 'Options must be a hash reference' if ref( $opts ) ne 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | 19 | my ( $fh, $close ) = _open_for_read( $file ); | |||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | 19 | my $buf = do { local $/; <$fh> }; | |||||||||||||||||||||||||||||||||||||||||
| 98 | 19 | ▶ | die "read failed" if not defined $buf; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | 19 | ▶ | if ( $close ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 19 | ▶ | close $fh or die "close: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | 19 | ▶ | return defined $opts | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | ? decode_smile( $buf, $opts ) | ||||||||||||||||||||||||||||||||||||||||||
| 106 | : decode_smile( $buf ); | ||||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | __END__ | ||||||||||||||||||||||||||||||||||||||||||