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

Hello Monks,

I need to create a temporary file in one of my module's Test::More scripts. I know there is new_tmpfile() function in IO::File. Is it windows-compatible? Is there a common way to create temp files in tests?


Thanks

Replies are listed 'Best First'.
Re: temporary files in .t scripts
by GrandFather (Saint) on Mar 24, 2008 at 22:54 UTC

    IO::File's new_tmpfile method should be a good choice for creating temporary files in a test suite. The documentation implies that it should be fine for Windows use and a simple test script certainly works as expected.


    Perl is environmentally friendly - it saves trees
Re: temporary files in .t scripts
by Burak (Chaplain) on Mar 24, 2008 at 23:07 UTC
    I think that File::Temp is the best tool for this task
Re: temporary files in .t scripts
by dragonchild (Archbishop) on Mar 25, 2008 at 02:06 UTC
    You've had two recommendations for File::Temp. Consider this a third - I use tempfiles in the test suites for DBM::Deep, PDF::Template, and Excel::Template. The common denominator is that I have a t/common.pm which manages the creation and destruction. You will probably want something similar.

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
      You'll laugh: I need a tempfile for DBM::Deep which is used by default in my module. :-) Thanks for your answer.
        Oooh. What does your module do? What do you use DBM::Deep for? Inquiring minds want to know!

        My criteria for good software:
        1. Does it work?
        2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
Re: temporary files in .t scripts
by kyle (Abbot) on Mar 25, 2008 at 01:55 UTC
Re: temporary files in .t scripts
by ack (Deacon) on Mar 25, 2008 at 04:45 UTC

    I use File::Temp all the time on my Windows XP machine. Burak's, kyle's and drangonchild's recommendations are 'on-target' from my humble perspective. Haven't tried GrandFather's recommended IO::File approach; but intend to look into it to see if it offers any advantages. Thanks, GrandFather.

    ack Albuquerque, NM
Re: temporary files in .t scripts (Directory::Scratch)
by lodin (Hermit) on Mar 25, 2008 at 13:28 UTC

    I haven't tried Directory::Scratch, but it looks pretty nice. It's specifically written to handle cross-platform issues with temporary test files. From Directory::Scratch:

    When writing test suites for modules that operate on files, it's often inconvenient to correctly create a platform-independent temporary storage space, manipulate files inside it, then clean it up when the test exits. /.../ This module aims to eliminate that problem by making it easy to do things right.
    It may be overkill if you only want to create only one file though.

    lodin