kenahoo / Path-Class

Cross-platform path specification manipulation
http://search.cpan.org/dist/Path-Class/
15 stars 28 forks source link

Path::Class::File->temp support #33

Open mrdvt92 opened 10 years ago

mrdvt92 commented 10 years ago

It would be nice to provide temp file support

my $file = Path::Class::File->temp;

On Unixy systems it would put the file in /tmp. On Windows systems it should do the right thing as well.

Example:

perl -e '

{ package Path::Class::FileX; use strict; use warnings; use base qw{Path::Class::File}; use File::Tempdir qw{};

sub temp { my $self = shift; #class or object my $tmpdir = File::Tempdir->new or die("Error: Could not create File::Tempdir object"); my $local_folder = $tmpdir->name or die("Error: Temporary directory not configured."); my $file = $self->new($local_folder => "file.tmp"); #folder is unique $file->{"__tmpdir"} = $tmpdir; #must keep tmpdir scope alive return $file; } }

my $location; { my $file=Path::Class::FileX->temp; print "$file\n"; $location="$file"; my $fh=$file->openw; print $fh "Hello World!\n"; $fh->close;

print $file->slurp; }

print "Exists\n" if -f $location; #file is out of scope so gone!

'

kenahoo commented 9 years ago

I like the idea. Would you be willing to work up a pull request?