Skeeve has asked for the wisdom of the Perl Monks concerning the following question:

Hi! I just noticed that perl -e 'use File::Temp qw/ tempdir /; print tempdir( "asd-XXXX.mbox")' fails with the message: Error in tempdir() using asd-XXXX.mbox: The template must contain at least 4 'X' characters

But there are 4X so why this message? Did I find a bug or is this behaviour documented?


s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
+.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e

Replies are listed 'Best First'.
Re: tempdir with template fails
by imp (Priest) on Dec 03, 2006 at 00:39 UTC
    From the perldocs for File::Temp:
    Trailing `X' characters are replaced with random letters to generate the filename. At least four `X' characters must be present at the end of the template.
    And the relevant code from the module:
    # Do it using substr - no reason to use a pattern match since # we know where we are looking and what we are looking for if (substr($template, $start - MINX + 1, MINX) ne 'X' x MINX) { ${$options{ErrStr}} = "The template must contain at least ". MINX . " 'X' characters\n"; return (); }
Re: tempdir with template fails
by bobf (Monsignor) on Dec 03, 2006 at 04:47 UTC

    As imp mentioned, this is behavior is documented. Unfortunately, the source code follows the docs. :-) I was hoping tempdir accepted a SUFFIX parameter just as tempfile does, but it does not (as of version 0.16).

    If you really want your temp dir to have an extension, you could still use File::Temp but it would require a bit more work.

    1. Find all existing dirs in your base dir and create a file for them. For example, if the dir was named 'dir1234.mbox' you could create a file with the name 'temp_dir1234.mbox'.
    2. Use tempfile to create a new (unique) temp file in the base dir using the template 'temp_XXXXXXX' and SUFFIX 'mbox'.
    3. Since the existing directory names match the file names, you can create a directory named 'temp_XXXXXXX.mbox' (for whatever XXXXXXX is) and know it is unique. (If you were worried about race conditions you could check to see if it existed first.)
    4. When you're done with the temp dir and it is deleted, make sure you delete the corresponding file as well.

    I'll be the first to say that the solution enumerated above is an ugly hack. If it were me, I'd use extensionless directory names or just check to see if it existed before I tried to create it. Do you really need the power of File::Temp for this? If you're not creating multiple temp dirs simultaneously this approach seems like overkill.

Re: tempdir with template fails
by Firefly258 (Beadle) on Dec 03, 2006 at 17:58 UTC
    I find renaming the directory works best.
    perl -MFile::Copy -MFile::Temp=tempdir -e 'move $_=tempdir( "asd-XXXX" + ), $_.".mbox";'



    perl -e '$,=$",$_=(split/\W/,$^X)[y[eval]]]+--$_],print+just,another,split,hack'er

      No.... What if (by accident) a tempdir is created which already exists as .mbox?

      My solution now is - as I don't need the directory at a specific place:
      1. Create a standard tempdir
      2. Create a fixed "name.mbox" directory within that directory

      Nevertheless: Thanks to you and all the others who answered.


      s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
      +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
        Sure, it was just a one-liner to demonstrate my approach. If i were to be more pragmatic, i would code around the various exceptions.

        Maybe something like this could be of help in this instance??
        #!/usr/bin/perl -Wl use strict; use File::Spec qw| catdir tmpdir |; my $tmpdir; sub repl { my @CHARS = ("a".."z", "0".."9"); local $_ = pop; s/ X / $CHARS[int rand $#CHARS] /gex; $_; } do { $tmpdir = File::Spec->catdir( File::Spec->tmpdir , repl ("asd-XXXX +.mbox") ); } until (!-e $tmpdir); print $tmpdir;


        perl -e '$,=$",$_=(split/\W/,$^X)[y[eval]]]+--$_],print+just,another,split,hack'er