NAME

CLI::Simple::TempDir - temporary directory creation and ownership for Role::Tiny consumers

SYNOPSIS

package My::Processor;

use Role::Tiny::With;

with 'CLI::Simple::TempDir';

sub process {
  my ($self) = @_;

  my $workspace = $self->create_temp_dir(
    accessor => 'workspace',
    cleanup  => 1,
  );

  # $self->get_workspace returns the path.
  return $workspace;
}

DESCRIPTION

This role allocates unique temporary directories using File::Temp. The consumer must be a blessed hash reference. Each consuming object owns its temporary directory objects; generated accessors expose only pathnames.

With cleanup => 1 (the default), destroying the owning object normally removes the directory and its contents, including files the application placed there. Do not use an automatically cleaned directory for output that must survive the owning object's destruction. cleanup => 0 leaves the directory in place.

Cleanup is not guaranteed after abrupt process termination. Retaining a reference to the owner delays cleanup, and changing a generated accessor never transfers ownership of the original directory.

METHODS

create_temp_dir

my $path = $self->create_temp_dir(
  accessor => 'workspace',
  dir      => '/tmp',
  template => 'myapp-XXXXXXXX',
  cleanup  => 1,
);

Creates a directory and returns its pathname. Supported attributes are accessor, no_accessors, cleanup, dir, and template. dir specifies the parent directory; it is not the generated directory. The latter two creation arguments map to DIR and TEMPLATE in File::Temp.

By default the role creates get_temp_dir() and set_temp_dir() in the consuming class. accessor changes the generated names. no_accessors => 1 disables accessor generation but still requires a hash-based object. Specifying both accessor and no_accessors is an error.

Multiple directories may be created by calling the method repeatedly. Use distinct accessor names to retain separate paths through accessors. All created directories remain independently owned by the same instance.