| 1 | package File::Tempdir; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use File::Temp qw(tempdir); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use File::Path; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | our $VERSION = '0.02'; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | File::Tempdir | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 SYNOPSYS | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | use File::Tempdir; | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | { | ||||||||||||||||||||||||||||
| 19 | my $tmpdir = File::Tempdir->new() | ||||||||||||||||||||||||||||
| 20 | my $dir = $tmpdir->name; | ||||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | # the directory has been trashed | ||||||||||||||||||||||||||||
| 23 | my $adir = 'any directory'; | ||||||||||||||||||||||||||||
| 24 | { | ||||||||||||||||||||||||||||
| 25 | my $tmpdir = File::Tempdir->new($dir) | ||||||||||||||||||||||||||||
| 26 | my $dir = $tmpdir->name; | ||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | # the directory has not been trashed | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | This module provide an object interface to tempdir() from L<File::Temp>. | ||||||||||||||||||||||||||||
| 33 | This allow to destroy the temporary directory as soon you don't need it | ||||||||||||||||||||||||||||
| 34 | anymore using the magic DESTROY() function automatically call be perl | ||||||||||||||||||||||||||||
| 35 | when the object is no longer reference. | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | If a value is passed to at object creation, it become only a container | ||||||||||||||||||||||||||||
| 38 | allowing to keep same code in your function. | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =head2 new(@options) | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | if @options is only one defined value, the directory is simply | ||||||||||||||||||||||||||||
| 45 | retain in memory and will not been trashed. | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | Otherwise, @options are same than tempdir() from L<File::Temp>. | ||||||||||||||||||||||||||||
| 48 | Refer to L<File::Temp> documentation to have options list. | ||||||||||||||||||||||||||||
| 49 | In this case, the directory will be trashed. | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | =cut | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 2 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||
| 54 | 2 | my ($class, @args) = @_; | |||||||||||||||||||||||||||
| 55 | 2 | ▶ | if (@args == 1 && defined($args[0])) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@args == 1 and defined $args[0]
MC/DC50%1/2
@args == 1 and defined $args[0]
@args == 1
defined $args[0]
Truth table66%2/3
A and B
A= @args == 1B= defined $args[0]
| |||||||||||||||||||||||||||||
| 56 | # Not a tempdir, simply to have same interface in code | ||||||||||||||||||||||||||||
| 57 | 1 | return bless({ dir => $args[0] }, $class); | |||||||||||||||||||||||||||
| 58 | } else { | ||||||||||||||||||||||||||||
| 59 | 1 | ▶ | my $dir = tempdir(@args) or return undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 1 | return bless({ tmpdir => $dir }, $class); | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 2 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 65 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||
| 66 | 2 | ▶ | if ($self->{tmpdir}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 1 | rmtree($self->{tmpdir}, 0, 0); | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | =head2 name | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | Return the name of the directory handle by the object. | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | =cut | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 2 | ▶ | sub name { | ||||||||||||||||||||||||||
|
Subroutine
name: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 78 | defined($_[0]->{tmpdir}) ? $_[0]->{tmpdir} : $_[0]->{dir} | ||||||||||||||||||||||||||||
| 79 | 2 | ▶ | } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 1 | 1; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | __END__ | ||||||||||||||||||||||||||||