| 1 | package File::Fu::Dir::Temp; | ||||||||||
| 2 | 13 | $VERSION = v0.0.8; | |||||||||
| 3 | |||||||||||
| 4 | 13 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 13 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 13 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | =begin shutup_pod_cover | ||||||||||
| 9 | |||||||||||
| 10 | =head2 clone | ||||||||||
| 11 | |||||||||||
| 12 | =head2 XXX | ||||||||||
| 13 | |||||||||||
| 14 | =end shutup_pod_cover | ||||||||||
| 15 | |||||||||||
| 16 | =cut | ||||||||||
| 17 | |||||||||||
| 18 | 13 | ▶ | use File::Fu::File::Temp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | 13 | *_validate = \&File::Fu::File::Temp::_validate; | |||||||||
| 20 | 13 | *XXX = \&File::Fu::File::Temp::XXX; | |||||||||
| 21 | |||||||||||
| 22 | =head1 NAME | ||||||||||
| 23 | |||||||||||
| 24 | File::Fu::Dir::Temp - temporary directories | ||||||||||
| 25 | |||||||||||
| 26 | =head1 SYNOPSIS | ||||||||||
| 27 | |||||||||||
| 28 | use File::Fu; | ||||||||||
| 29 | my $dir = File::Fu->temp_dir; | ||||||||||
| 30 | |||||||||||
| 31 | =cut | ||||||||||
| 32 | |||||||||||
| 33 | 13 | ▶ | use base 'File::Fu::Dir'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 34 | 13 | ▶ | use overload ( | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.4
| |||||||||||
| 35 | 1 | ▶ | '/=' => sub {croak("cannot mutate a temp dir");}, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 36 | 0 | ▶ | '+=' => sub {croak("cannot mutate a temp dir");}, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 37 | 0 | ▶ | '.=' => sub {croak("cannot mutate a temp dir");}, | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 38 | 13 | ); | |||||||||
| 39 | |||||||||||
| 40 | 13 | ▶ | use Class::Accessor::Classy; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 41 | 13 | rs auto_delete => \(my $set_auto_delete); | |||||||||
| 42 | 13 | rs dir_class => \(my $set_dir_class); | |||||||||
| 43 | 13 | ▶ | no Class::Accessor::Classy; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 44 | |||||||||||
| 45 | =head2 new | ||||||||||
| 46 | |||||||||||
| 47 | my $tmp = File::Fu::Dir::Temp->new($dir, 'foo'); | ||||||||||
| 48 | |||||||||||
| 49 | =cut | ||||||||||
| 50 | |||||||||||
| 51 | { | ||||||||||
| 52 | 13 | my %argmap = ( | |||||||||
| 53 | nocleanup => [UNLINK => 0], | ||||||||||
| 54 | ); | ||||||||||
| 55 | 5 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 56 | 5 | my $proto = shift; | |||||||||
| 57 | 5 | ▶ | if(ref($proto)) { # calls to subdir, etc are not in the Temp class | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | 2 | return $proto->dir_class->new(@_); | |||||||||
| 59 | } | ||||||||||
| 60 | 3 | my $class = $proto; | |||||||||
| 61 | #warn "args: @_"; | ||||||||||
| 62 | 3 | my ($dir, $send, $opt) = $class->_validate(\%argmap, @_); | |||||||||
| 63 | |||||||||||
| 64 | #warn "dir: $dir"; | ||||||||||
| 65 | #warn "opts: @$send"; | ||||||||||
| 66 | 3 | my $temp = File::Temp::tempdir(@$send); | |||||||||
| 67 | 3 | my $self = $class->SUPER::new($temp); | |||||||||
| 68 | 3 | $self->{$_} = $opt->{$_} for(keys(%$opt)); | |||||||||
| 69 | 3 | $self->{_proc} = $$; | |||||||||
| 70 | 3 | $self->$set_dir_class(ref($dir)); | |||||||||
| 71 | |||||||||||
| 72 | 3 | return($self); | |||||||||
| 73 | }} # end subroutine new definition | ||||||||||
| 74 | ######################################################################## | ||||||||||
| 75 | |||||||||||
| 76 | =head2 chdir | ||||||||||
| 77 | |||||||||||
| 78 | my $dir = $dir->chdir; | ||||||||||
| 79 | |||||||||||
| 80 | =cut | ||||||||||
| 81 | |||||||||||
| 82 | 13 | ▶ | sub chdir { | ||||||||
|
Subroutine
chdir: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 83 | 1 | my $self = shift; | |||||||||
| 84 | |||||||||||
| 85 | 1 | my $dir = $self->SUPER::chdir; | |||||||||
| 86 | 1 | $dir->{temp_parent} = $self; | |||||||||
| 87 | 1 | return($dir); | |||||||||
| 88 | } # chdir ############################################################## | ||||||||||
| 89 | |||||||||||
| 90 | =for nit head2 clone | ||||||||||
| 91 | Because clone doesn't call new :-/ | ||||||||||
| 92 | $not_temp = $temp->clone; | ||||||||||
| 93 | |||||||||||
| 94 | =cut | ||||||||||
| 95 | |||||||||||
| 96 | 2 | ▶ | sub clone { | ||||||||
|
Subroutine
clone: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 97 | 2 | my $self = shift; | |||||||||
| 98 | 2 | $self = $self->SUPER::clone; | |||||||||
| 99 | 2 | bless($self, $self->dir_class); | |||||||||
| 100 | } # end subroutine clone definition | ||||||||||
| 101 | ######################################################################## | ||||||||||
| 102 | |||||||||||
| 103 | =head2 rename | ||||||||||
| 104 | |||||||||||
| 105 | Same as the base rename(), but promotes the temp dir to a regular Dir | ||||||||||
| 106 | object (prevents any cleanup actions.) | ||||||||||
| 107 | |||||||||||
| 108 | $temp = $temp->rename($dest); | ||||||||||
| 109 | |||||||||||
| 110 | =cut | ||||||||||
| 111 | |||||||||||
| 112 | 0 | ▶ | sub rename { | ||||||||
|
Subroutine
rename: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 113 | 0 | my $self = shift; | |||||||||
| 114 | 0 | my $dir_class = $self->dir_class; | |||||||||
| 115 | 0 | $self = $self->SUPER::rename(@_); | |||||||||
| 116 | 0 | bless($self, $dir_class); | |||||||||
| 117 | 0 | return($self); | |||||||||
| 118 | } | ||||||||||
| 119 | |||||||||||
| 120 | # TODO File::Fu->temp_dir->chdir causes immediate deletion? | ||||||||||
| 121 | |||||||||||
| 122 | =head2 nocleanup | ||||||||||
| 123 | |||||||||||
| 124 | Disable autocleanup. | ||||||||||
| 125 | |||||||||||
| 126 | $dir->nocleanup; | ||||||||||
| 127 | |||||||||||
| 128 | =cut | ||||||||||
| 129 | |||||||||||
| 130 | # XXX I think this is named wrong -- should probably just delete the | ||||||||||
| 131 | # dependency on File::Temp because I can't override that END block | ||||||||||
| 132 | 0 | ▶ | sub nocleanup { | ||||||||
|
Subroutine
nocleanup: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 133 | 0 | my $self = shift; | |||||||||
| 134 | 0 | $self->$set_auto_delete(0); | |||||||||
| 135 | } # end subroutine nocleanup definition | ||||||||||
| 136 | ######################################################################## | ||||||||||
| 137 | |||||||||||
| 138 | =head2 DESTROY | ||||||||||
| 139 | |||||||||||
| 140 | Called automatically when the object goes out of scope. | ||||||||||
| 141 | |||||||||||
| 142 | $dir->DESTROY; | ||||||||||
| 143 | |||||||||||
| 144 | =cut | ||||||||||
| 145 | |||||||||||
| 146 | 3 | ▶ | sub DESTROY { | ||||||||
|
Subroutine
DESTROY: called CC=4 SCAR=14.3
| |||||||||||
| 147 | 3 | my $self = shift; | |||||||||
| 148 | |||||||||||
| 149 | # ? should this have: | ||||||||||
| 150 | 3 | ▶ | return unless($self->auto_delete); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 151 | |||||||||||
| 152 | # forked case | ||||||||||
| 153 | 3 | ▶ | return unless($$ == $self->{_proc}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 154 | |||||||||||
| 155 | 3 | my $string = $self->stringify; | |||||||||
| 156 | #warn "DESTROY ($$/$self->{_proc}", $string; | ||||||||||
| 157 | # XXX overload stops operating in DESTROY()? | ||||||||||
| 158 | |||||||||||
| 159 | 3 | ▶ | die("$string does not exist") unless(-d $string); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 160 | 3 | $self->remove; | |||||||||
| 161 | 3 | $self->{auto_delete} = 0; | |||||||||
| 162 | } # end subroutine DESTROY definition | ||||||||||
| 163 | ######################################################################## | ||||||||||
| 164 | |||||||||||
| 165 | =head1 AUTHOR | ||||||||||
| 166 | |||||||||||
| 167 | Eric Wilhelm @ <ewilhelm at cpan dot org> | ||||||||||
| 168 | |||||||||||
| 169 | http://scratchcomputing.com/ | ||||||||||
| 170 | |||||||||||
| 171 | =head1 BUGS | ||||||||||
| 172 | |||||||||||
| 173 | If you found this module on CPAN, please report any bugs or feature | ||||||||||
| 174 | requests through the web interface at L<http://rt.cpan.org>. I will be | ||||||||||
| 175 | notified, and then you'll automatically be notified of progress on your | ||||||||||
| 176 | bug as I make changes. | ||||||||||
| 177 | |||||||||||
| 178 | If you pulled this development version from my /svn/, please contact me | ||||||||||
| 179 | directly. | ||||||||||
| 180 | |||||||||||
| 181 | =head1 COPYRIGHT | ||||||||||
| 182 | |||||||||||
| 183 | Copyright (C) 2008 Eric L. Wilhelm, All Rights Reserved. | ||||||||||
| 184 | |||||||||||
| 185 | =head1 NO WARRANTY | ||||||||||
| 186 | |||||||||||
| 187 | Absolutely, positively NO WARRANTY, neither express or implied, is | ||||||||||
| 188 | offered with this software. You use this software at your own risk. In | ||||||||||
| 189 | case of loss, no person or entity owes you anything whatsoever. You | ||||||||||
| 190 | have been warned. | ||||||||||
| 191 | |||||||||||
| 192 | =head1 LICENSE | ||||||||||
| 193 | |||||||||||
| 194 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 195 | under the same terms as Perl itself. | ||||||||||
| 196 | |||||||||||
| 197 | =cut | ||||||||||
| 198 | |||||||||||
| 199 | # vi:ts=2:sw=2:et:sta | ||||||||||
| 200 | 13 | 1; | |||||||||