View on
MetaCPAN is shutting down
For details read Perl NOC. After June 25th this page will redirect to
Kent Fredric > File-Tempdir-ForPackage-0.1.1 > File::Tempdir::ForPackage



Annotate this POD


View/Report Bugs
Module Version: v0.1.1   Source   Latest Release: File-Tempdir-ForPackage-1.000002


File::Tempdir::ForPackage - Easy temporary directories associated with packages.


version 0.1.1


This is mostly an interface wrapper for File::Temp::tempdir, stealing code from File::Tempdir;

And this is designed to solve this simply.

use File::TempDir::ForPackage;

  my $tempdir = File::TempDir::ForPackage->new( package => __PACKAGE__ , use_version => 1 );
  my $dir = $tempdir->dir();

do shit in `$dir` $dir on Linux will be something like /tmp/perl-Some-Package-maybewith-a-VERSION-AFG14561/ so if it crashes and leaves a tempdir behind, you will know who left that tempdir behind and have a way of cleaning it up.

When $tempdir is destroyed, $dir will be cleaned;



Is there for people who don't trust scope auto-cleansing and want to know when the dir is reaped.

Additionally, this code can be run in a tight loop creating and destroying lots of similary named tempdirs without risk of conflict.

  for my $i ( 0 .. 30  ) {
    $dir->run_once_in(sub {
      system 'find $PWD';

This emits something like:


Except of course, with a package of your choosing, and possibly that packages version.



Toggle the preservation of the tempdir after it goes out of scope or is otherwise indicated for cleaning.

  $instance->preserve(); # tempdir is now preserved after cleanup
  $instance->preserve(0); # tempdir is purged at cleanup
  $instance->preserve(1); # tempdir is preserved after cleanup

Note that in run_once_in, a new tempdir is created and set for this modules consumption for each run of run_once_in, regardless of this setting. All this setting will do, when set, will prevent each instance being reaped from the filesystem.


  for( 1..10 ){ 


Will create 10 temporary directories on your filesystem and not reap them.


Return a path string to the created temporary directory

  my $path = $instance->dir


Detach the physical file system directory from being connected to this object.

If preserve is not set, then this will mean dir will be reaped, and the dir attribute will be reset, ready to be re-initialized the next time it is needed.

If preserve is set, then from the outside codes persective its basically the same, dir is reset, waiting for re-initialization next time it is needed. Just dir is not reaped.



Vivifies a temporary directory for the scope of the passed sub.

    # temporary directory is created before this code runs.
    # Cwd::getcwd is now inside the temporary directory.

  # temporary directory is reset, and possibly reaped.

You can call this method repeatedly, and you'll get a seperate temporary directory each time.


Hook to trigger automatic cleansing when the object is lost out of scope, as long as preserve is unset.



The package to report as being associated with. This really can be any string, as its sanitised and then used as a path part.

If not specified, will inspect caller

  my $instance = CLASS->new(
    package => 'Something::Here',

Note: If you want with_version to work properly, specifying a valid package name will be helpful.


Include the version from package->VERSION() in the tempdir path.

Defaults to false.

  my $instance = CLASS->new(
    with_version => 1,


Include time in the tempdir path.

Defaults to false.

  my $instance = CLASS->new(
    with_timestamp => 1,


Include $$ in the tempdir path.

Defaults to false.

  my $instance = CLASS->new(
    with_pid => 1,


The number of characters of randomness to include in the tempdir template.

Defaults to 8. Must be no lower than 4.

  my $instance = CLASS->new(
    num_random => 5,



Internal boolean for tracking the _preserve state.


Internal File::Tempdir path.



Initializer for _dir which creates a temporary directory based on the passed parameters.



Scrape garbage out of the 'package' field for use in filesystem tokens.


Scrape garbage out of versions for use in filesystem tokens.


Kent Fredric <>


This software is copyright (c) 2012 by Kent Fredric <>.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

syntax highlighting: