File Coverage

blib/lib/File/Tempdir.pm
Criterion Covered Total %
statement 21 21 100.0
branch 7 8 87.5
condition 2 3 66.6
subroutine 7 7 100.0
pod 2 2 100.0
total 39 41 95.1


line stmt bran cond sub pod time code
1             package File::Tempdir;
2              
3 1     1   828 use strict;
  1         2  
  1         40  
4 1     1   6 use warnings;
  1         2  
  1         37  
5 1     1   21 use File::Temp qw(tempdir);
  1         3  
  1         68  
6 1     1   6 use File::Path;
  1         2  
  1         290  
7              
8             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.
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.
48             Refer to L documentation to have options list.
49             In this case, the directory will be trashed.
50              
51             =cut
52              
53             sub new {
54 2     2 1 1083 my ($class, @args) = @_;
55 2 100 66     15 if (@args == 1 && defined($args[0])) {
56             # Not a tempdir, simply to have same interface in code
57 1         7 return bless({ dir => $args[0] }, $class);
58             } else {
59 1 50       6 my $dir = tempdir(@args) or return undef;
60 1         638 return bless({ tmpdir => $dir }, $class);
61             }
62             }
63              
64             sub DESTROY {
65 2     2   914 my ($self) = @_;
66 2 100       11 if ($self->{tmpdir}) {
67 1         452 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             sub name {
78 2 100   2 1 659 defined($_[0]->{tmpdir}) ? $_[0]->{tmpdir} : $_[0]->{dir}
79             }
80              
81             1;
82              
83             __END__