* Frank D. Cringle <[EMAIL PROTECTED]> [2002-07-07 14:10]:
> Mark Fowler <[EMAIL PROTECTED]> writes:
> > File::Temp? It's core too...
>
> File::Temp is concerned with creating and opening temporary files. I
> was thinking of something that does that in a given directory and
> returns a magic filehandle (objectified?, tied?) that renames the file
> when it is closed and that deletes the file if the process terminates
> before it is closed.
This is an interesting idea. Hmmm...
package IO::MagicFile;
use strict;
use IO::File;
use POSIX qw(tmpnam);
sub new {
my $type = shift;
my $class = ref($type) || $type || "IO::MagicFile";
my $fh = IO::File->new;
my $self = bless {
'_FH' => $fh,
'_TMPNAM' => undef,
'_FILENAME' => undef,
} => $class;
if (@_) {
$self->open(@_)
or return undef;
}
return $self;
}
sub open {
my ($self, $file) = @_;
my $tmpnam = tmpnam;
if (@_ > 2) {
my ($mode, $perms) = @_[2, 3];
if ($mode =~ /^\d+$/) {
defined $perms or $perms = 0666;
return sysopen($self->{ '_FH' }, $tmpnam, $mode, $perms);
}
$file = IO::Handle::_open_mode_string($mode) . " $file\0";
}
$self->{ '_TMPNAM' } = $tmpnam;
$self->{ '_FILENAME' } = $file;
open($self->{ '_FH' }, $file);
}
use vars qw($AUTOLOAD);
sub AUTOLOAD {
my $self = shift;
my $a = $AUTOLOAD;
$a =~ s/.*:://;
my $fh = $self->{ '_FH' };
$fh->$a(@_);
}
sub DESTROY {
my $self = shift;
my $tmpnam = $self->{ '_TMPNAM' };
my $filename = $self->{ '_FILENAME' };
rename $tmpnam, $filename;
}
1;
My *really* simple tests indicate that this works as expected, though
I'm sure there are tons of problems with it.
(darren)
--
The road to Hell is paved with Bibles.