1
package File::Fu::File::Temp;
10
File::Fu::File::Temp - temporary files
15
my $handle = File::Fu->temp_file;
20
# XXX should be File::Fu::Handle;
21
#use base 'File::Temp';
25
The directory argument is required, followed by an optional template
26
argument and/or flags. The template may contain some number of 'X'
27
characters. If it does not, ten of them will be appended.
29
my $handle = File::Fu::File::Temp->new($dir, 'foo');
30
my $file = $handle->name;
32
By default, the file will be deleted when the handle goes out of scope.
33
Optionally, it may be deleted immediately after creation or just not
36
my $handle = File::Fu::File::Temp->new($dir, 'foo', -secure);
38
my $handle = File::Fu::File::Temp->new($dir, -noclean);
39
# also $handle->noclean;
45
Delete the named file (if the OS supports it) immediately after opening.
47
Calling name() on this sort of handle throws an error.
51
Don't attempt to remove the file when the $handle goes out of scope.
60
nocleanup => [UNLINK => 0],
64
my $class = ref($proto) || $proto;
65
my ($dir, $send, $opt) = $class->_validate(\%argmap, @_);
69
$self = File::Temp::tempfile(@$send);
70
$class .= '::HasNoFileName';
73
($self, $fn) = File::Temp::tempfile(@$send);
74
${*$self} = $dir->file_class->new($fn);
79
}} # end subroutine new definition
80
########################################################################
82
=for internal head2 _validate
83
my ($dir, $send, $opt) = $class->_validate(\%map, @_);
89
my %argmap = %{shift(@_)};
91
croak("invalid directory '$dir' ")
92
unless(eval {$dir->can('e')} and $dir->e);
96
for(my $i = 0; $i < @opt; $i++) {
97
($opt[$i] =~ s/^-//) or next;
98
my ($key) = splice(@opt, $i, 1); $i--;
99
my $do = $argmap{$key} or croak("invalid argument '$key'");
104
my $template = shift(@opt);
105
croak("invalid arguments '@opt'") if(@opt);
107
$template .= $class->XXX unless($template =~ m/X/);
108
# XXX File::Temp specific
109
unshift(@send, $template);
111
$opt{auto_delete} = ! delete($opt{nocleanup});
113
push(@send, DIR => "$dir");
115
return($dir, \@send, \%opt);
116
} # end subroutine _validate definition
117
########################################################################
121
my $file_obj = $handle->name;
128
} # end subroutine name definition
129
########################################################################
142
$opt{auto_delete} = 0;
143
} # end subroutine nocleanup definition
144
########################################################################
148
Called automatically when the handle goes out of scope.
157
return if($opt{secure} or ! $opt{auto_delete});
159
} # end subroutine DESTROY definition
160
########################################################################
162
use constant XXX => 'X'x10;
166
Eric Wilhelm @ <ewilhelm at cpan dot org>
168
http://scratchcomputing.com/
172
If you found this module on CPAN, please report any bugs or feature
173
requests through the web interface at L<http://rt.cpan.org>. I will be
174
notified, and then you'll automatically be notified of progress on your
175
bug as I make changes.
177
If you pulled this development version from my /svn/, please contact me
182
Copyright (C) 2008 Eric L. Wilhelm, All Rights Reserved.
186
Absolutely, positively NO WARRANTY, neither express or implied, is
187
offered with this software. You use this software at your own risk. In
188
case of loss, no person or entity owes you anything whatsoever. You
193
This program is free software; you can redistribute it and/or modify it
194
under the same terms as Perl itself.
198
# vi:ts=2:sw=2:et:sta