Skip to content

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

@mrdvt92

Description

@mrdvt92

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!

'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions