Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mrdvt92 opened this issue Oct 13, 2014 · 1 comment
Open

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

mrdvt92 opened this issue Oct 13, 2014 · 1 comment

Comments

@mrdvt92
Copy link

mrdvt92 commented Oct 13, 2014

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
Copy link
Owner

kenahoo commented Nov 11, 2014

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants