NAME
CLI::Simple::TempFile - temporary file creation and ownership for Role::Tiny consumers
SYNOPSIS
package My::Processor;
use Role::Tiny::With;
with 'CLI::Simple::TempFile';
sub process {
my ($self) = @_;
my $path = $self->create_temp_file(
accessor => 'report',
suffix => '.csv',
cleanup => 1,
);
# $self->get_report returns the same path.
return $path;
}
DESCRIPTION
This role creates temporary files via File::Temp. The consuming object owns the underlying file objects until it is destroyed. When cleanup is true (the default), the temporary file is removed when its owner is destroyed under normal Perl object-lifetime semantics. cleanup => 0 leaves it on disk. Cleanup cannot be guaranteed after abrupt termination.
Note: Consuming class must be a blessed hash.
METHODS
create_temp_file
my $path = $self->create_temp_file(
accessor => 'report',
dir => '/tmp',
suffix => '.csv',
cleanup => 1,
);
Creates and returns the path of a new temporary file. Supported attributes are accessor, no_accessors, cleanup, dir, suffix, and template (the latter three map to the corresponding File::Temp arguments). By default, generates get_temp_file and set_temp_file methods in the consuming class; accessor changes the name.
The consuming object must be a blessed hash reference. no_accessors => 1 disables accessor generation but does not change this requirement.
An accessor contains the pathname, not the file object. Changing the accessor does not transfer file ownership or alter cleanup behavior. Multiple calls are supported, but callers should use distinct accessor names when they want to retain multiple generated paths.