package File::Temp::Trace;

=head1 NAME

File::Temp::Trace - Trace the creation of temporary files

=head1 VERSION

Version 0.02


our $VERSION = '0.02';

=begin readme


The following packages are required:


=end readme


    package MyPkg;

    use File::Temp::Trace;

    my $tmp = File::Temp::Trace->tempdir();

    print STDERR "New temporary directory ${tmp} created.";

    sub create_file : skip_temp_log {
	my ($tmp, $ext) = @_;
	return $tmp->tempfile( suffix => $ext );

    sub create_text {
	my ($tmp, $ext) = @_;
	return create_file($tmp, '.txt');

    my $fh = create_text($tmp);

    # $fh->filename will be named "MyPkg-create_text-XXXXXXXX.txt",
    # where XXXXXXXX is a unique string.


This module allows you to trace the creation of temporary files. By
default, these files are all created in the same directory, and their
names are prefixed by the name of the function or method that created

You can optionally log the creation of temporary files with a stack
trace as well.


use strict;
use warnings;

use self;

use overload
    '""' => \&dir;

use Attribute::Handlers;
use Carp qw( longmess );
use File::Path qw( make_path );
use File::Spec;
use File::Temp ();
use Scalar::Util qw( refaddr );

    %File::Temp::Trace::SkipName = ( );

sub UNIVERSAL::skip_temp_log : ATTR(CODE) {
  my ($pkg, $sym, $ref, $attr, $data) = @_;
  $File::Temp::Trace::SkipName{substr($$sym,1)} = $data;

my %LogFiles = ( );

sub _name_to_template {
    my ($name) = @_;
    $name =~ s/\:\:/-/g;
    $name = "UNKNOWN", if (($name eq "") || ($name eq "(eval)"));
    return "${name}-XXXXXXXX";

=for readme stop

Methods are documented below:

=head2 tempdir

  $tmp = File::Temp::Trace->tempdir(%options);

Creates a new temporary directory and returns a blessed reference to
the name of that temporary directory.

The following options may be used:


=item cleanup

Delete the directory and contents once the object is
destroyed. True by default.

=item template

A template for the name of directory. By default, it is
C<File-Temp-Trace-XXXXXXXX>, where C<XXXXXXXX> is a unique string.

The template name must end with at least C<XXXX>.

=item dir

The parent directory of the temporary directory. By default, it is in
the system temporary directory.

=item log

Create a log file that gives the time that a temporary file was
created, and a L<Carp::longmess> stack trace of the calling methods
that created it.

Note that if L</cleanup> is true, then the log file will be deleted
when the object is destroyed.



sub tempdir {
    my $class = shift || __PACKAGE__;

    my %opts = @args;

    my %ftopts = ( CLEANUP => 1, TEMPLATE => _name_to_template(__PACKAGE__), TMPDIR => 1 );
    foreach my $o (qw( cleanup template tmpdir dir )) {
	$ftopts{ uc($o) } = $opts{$o}, if (exists $opts{$o});

    $self = \ File::Temp->newdir($ftopts{TEMPLATE}, %ftopts);
    bless $self, $class;

    if ($opts{log}) {
	$LogFiles{ refaddr $self } = File::Temp->new( TEMPLATE => _name_to_template(__PACKAGE__), DIR => $self->dir, SUFFIX => ".log", UNLINK => 0 );

    return $self;

=head2 dir

  $path = $tmp->dir;

The pathname of the temporary directory created by L</tempdir>.

Note that the object is overloaded to return the pathname on


sub dir {
    return ${$self};

=head2 logfile

  $fh = $tmp->logfile;

Returns the filehandle of the log file, or C<undef> if the C<log>
option was not specified in the constructor.


sub logfile {
    return $LogFiles{ refaddr $self };

=head2 file

  $fh = $tmp->file(%options);

Creates a new temporary file in L</dir>, and returns a filehandle.

Note that unlike the corresponding method in L<File::Temp>, it does
not also return a filename.  To obtain a filename, use


The file is created using L<File::Temp>, so other methods from
L<File::Temp> may be used to query or manipulate the file.

The name of the file is of the form C<CALLER-XXXXXXXX> (plus any
suffix, if given as an option---see below), where C<CALLER> is the
name of the function of method that called L</file> and C<XXXXXXXX> is
a unique string.  This helps with debugging by making it easier to
identify which temporary file in L</dir> was created by a particular

In the case where a single method or function is used to create a
particular type of file, and is called by several other methods or
functions, it can be tagged with the C<skip_temp_log> attribute, so that
the name of the caller will come from further down the call stack. For

  sub create_file : skip_temp_log {

  sub fun_a {

  sub fun_b {

In this case, the two temporary files will be labelled with C<fun_a>
and C<fun_b> rather than both with C<create_file>.

The following options may be used.


=item unlink

If set to true, delete the file when the filehandle is destroyed. This
is set disabled by default, since the parent temporary directory is
normally set to be deleted.

=item suffix

The suffix (or extension) of the file.

=item exlock

The exclusive lock flag. True by default.

=item log

Create a separate log file when this file is created. The log file has
the same filename as the this file, plug the C<.log> suffix.

(In theory this is unsafe, as it does not ensure that a file with the
same name exists, though such a case in unlikely.)

=item dir

Create a subdirectory in the L</dir> directory, if it does not already
exist, and put the temporary file in there.


=head2 tempfile

  $fh = tempfile(%options);

This is an alias of L</file>.


sub tempfile {
    my $level = 1;
    my @frame = ( );
    my $name;
    do {
	@frame = caller($level++);
	$name   = $frame[3] || "";
    } while ($name && (exists $File::Temp::Trace::SkipName{$name}));

    my %opts = @args;

    my %ftopts = ( UNLINK => 0, TEMPLATE => _name_to_template($name), DIR => $self->dir, EXLOCK => 1 );
    foreach my $o (qw( unlink suffix exlock )) {
	$ftopts{ uc($o) } = $opts{$o}, if (exists $opts{$o});

    if (exists $opts{dir}) {
	$ftopts{DIR} = File::Spec->catfile(File::Spec->splitdir($self->dir), File::Spec->splitdir($opts{dir}));

    my $fh = File::Temp->new(%ftopts);
    if ((my $lh = $self->logfile) || ($opts{log})) {
	my $ts  = sprintf("[%s]", (scalar gmtime()));
	my $msg = sprintf("%s File %s created%s", $ts, $fh->filename, longmess());
	$msg =~ s/\n(.)/\n$ts $1/g;

	if ($lh) { print $lh $msg; }
	if ($opts{log}) {
	    open my $fhlh, sprintf(">%s.log", $fh->filename);
	    print $fhlh $msg;
	    close $fhlh;
    return $fh;

=begin readme


=for readme include file=Changes type=text

=end readme

=for readme continue

=head1 SEE ALSO


=head1 AUTHOR

Robert Rothenberg, C<< <> >>

=for readme stop

=head1 BUGS

Please report any bugs or feature requests to
C<>, or through the web interface at

=head1 SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc File::Temp::Trace

You can also look for information at:

=over 4

=item * RT: CPAN's request tracker


=item * AnnoCPAN: Annotated CPAN documentation


=item * CPAN Ratings


=item * Search CPAN


=item * GitHub



=for readme continue


Copyright 2011 Robert Rothenberg.

This program is free software; you can redistribute it and/or modify it
under the terms of either: the GNU General Public License as published
by the Free Software Foundation; or the Artistic License.

See for more information.