| 1 | package CLI::Simple::TempFile; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 18 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 18 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 18 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 18 | ▶ | use File::Temp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 18 | ▶ | use Role::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 18 | ▶ | use Scalar::Util qw(blessed reftype); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 18 | my %GENERATED_ACCESSORS; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 18 | our $VERSION = '2.6.0'; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | ######################################################################## | ||||||||||||||||||||||||||||
| 16 | 16 | ▶ | sub create_temp_file { | ||||||||||||||||||||||||||
|
Subroutine
create_temp_file: called CC=25 SCAR=32.3
Pod
documented
| |||||||||||||||||||||||||||||
| 17 | ######################################################################## | ||||||||||||||||||||||||||||
| 18 | 16 | my ( $self, %args ) = @_; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 16 | ▶ | croak 'create_temp_file requires an object instance' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | if !blessed($self); | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 16 | ▶ | croak 'create_temp_file requires a hash-based object' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | if reftype($self) ne 'HASH'; | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 16 | ▶ | my $no_accessors = delete $args{no_accessors} // 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
delete $args{"no_accessors"} // 0
MC/DC100%1/1
delete $args{"no_accessors"} // 0
delete $args{"no_accessors"}
Truth table100%2/2
A
A= delete $args{"no_accessors"}
| |||||||||||||||||||||||||||||
| 27 | 16 | ▶ | my $cleanup = delete $args{cleanup} // 1; | ||||||||||||||||||||||||||
|
Condition100%2/2
delete $args{"cleanup"} // 1
MC/DC100%1/1
delete $args{"cleanup"} // 1
delete $args{"cleanup"}
Truth table100%2/2
A
A= delete $args{"cleanup"}
| |||||||||||||||||||||||||||||
| 28 | 16 | my $has_accessor = exists $args{accessor}; | |||||||||||||||||||||||||||
| 29 | 16 | my $accessor = delete $args{accessor}; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 16 | ▶ | croak 'accessor and no_accessors cannot be combined' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$no_accessors and $has_accessor
MC/DC100%2/2
$no_accessors and $has_accessor
$no_accessors
$has_accessor
Truth table100%3/3
A and B
A= $no_accessorsB= $has_accessor
| |||||||||||||||||||||||||||||
| 32 | if $no_accessors && $has_accessor; | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 15 | ▶ | $accessor //= 'temp_file'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$accessor //= "temp_file"
MC/DC100%1/1
$accessor //= "temp_file"
$accessor
Truth table100%2/2
A
A= $accessor
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 15 | ▶ | croak 'cleanup must be 0 or 1' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | if $cleanup !~ /\A[01]\z/; | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 14 | ▶ | croak 'no_accessors must be 0 or 1' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | if $no_accessors !~ /\A[01]\z/; | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 14 | my %supported = map { $_ => 1 } qw(dir suffix template); | |||||||||||||||||||||||||||
| 43 | 14 | foreach my $key ( keys %args ) { | |||||||||||||||||||||||||||
| 44 | croak sprintf q{Unknown temporary file attribute: %s}, $key | ||||||||||||||||||||||||||||
| 45 | 3 | ▶ | if !exists $supported{$key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 13 | my $class = ref $self; | |||||||||||||||||||||||||||
| 49 | 13 | ▶ | if ( !$no_accessors ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 11 | ▶ | croak sprintf q{Invalid accessor name: %s}, $accessor | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | if $accessor !~ /\A[a-zA-Z_]\w*\z/; | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 10 | foreach my $prefix (qw(get set)) { | |||||||||||||||||||||||||||
| 54 | 20 | my $method = $prefix . '_' . $accessor; | |||||||||||||||||||||||||||
| 55 | croak sprintf q{Accessor already exists: %s}, $method | ||||||||||||||||||||||||||||
| 56 | 20 | ▶ | if $self->can($method) && !$GENERATED_ACCESSORS{$class}{$method}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->can($method) and not $GENERATED_ACCESSORS{$class}{$method}
MC/DC0%0/2
$self->can($method) and not $GENERATED_ACCESSORS{$class}{$method}
$self->can($method)
not $GENERATED_ACCESSORS{$class}{$method}
Truth table66%2/3
A and not(B)
A= $self->can($method)B= not $GENERATED_ACCESSORS{$class}{$method}
| |||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 12 | my %file_args = ( UNLINK => $cleanup ); | |||||||||||||||||||||||||||
| 61 | 12 | ▶ | $file_args{DIR} = $args{dir} if exists $args{dir}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 12 | ▶ | $file_args{SUFFIX} = $args{suffix} if exists $args{suffix}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 12 | ▶ | $file_args{TEMPLATE} = $args{template} if exists $args{template}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 12 | my $file = File::Temp->new(%file_args); | |||||||||||||||||||||||||||
| 66 | 12 | my $path = $file->filename; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | # Retain the File::Temp object, not merely its path. The accessor can | ||||||||||||||||||||||||||||
| 69 | # later change without affecting which file is cleaned up. | ||||||||||||||||||||||||||||
| 70 | 12 | ▶ | $self->{_cli_simple_temp_files} //= []; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"_cli_simple_temp_files"} //= []
MC/DC100%1/1
$self->{"_cli_simple_temp_files"} //= []
$self->{"_cli_simple_temp_files"}
Truth table100%2/2
A
A= $self->{"_cli_simple_temp_files"}
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 12 | push @{ $self->{_cli_simple_temp_files} }, $file; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 12 | ▶ | if ( !$no_accessors ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 10 | foreach my $prefix (qw(get set)) { | |||||||||||||||||||||||||||
| 76 | 20 | my $method = $prefix . '_' . $accessor; | |||||||||||||||||||||||||||
| 77 | 20 | ▶ | if ( !$GENERATED_ACCESSORS{$class}{$method} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 18 | ▶ | no strict 'refs'; ## no critic (ProhibitNoStrict) | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 79 | 14 | ▶ | if ( $prefix eq 'get' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 7 | ▶ | *{"${class}::${method}"} = sub { return $_[0]->{$accessor}; }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | else { | ||||||||||||||||||||||||||||
| 83 | 7 | ▶ | *{"${class}::${method}"} = sub { $_[0]->{$accessor} = $_[1]; return $_[0]->{$accessor}; }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | 14 | $GENERATED_ACCESSORS{$class}{$method} = 1; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | 10 | my $setter = 'set_' . $accessor; | |||||||||||||||||||||||||||
| 89 | 10 | $self->$setter($path); | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 12 | return $path; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 18 | 1; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | __END__ | ||||||||||||||||||||||||||||