Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

How do I make a temporary file name?

by faq_monk (Initiate)
on Oct 08, 1999 at 00:23 UTC ( #647=perlfaq nodetype: print w/replies, xml ) Need Help??

Current Perl documentation can be found at perldoc.perl.org.

Here is our local, out-dated (pre-5.6) version:

Use the new_tmpfile class method from the IO::File module to get a filehandle opened for reading and writing. Use this if you don't need to know the file's name.

        use IO::File;
    $fh = IO::File->new_tmpfile()
            or die "Unable to make new temporary file: $!";

Or you can use the tmpnam function from the POSIX module to get a filename that you then open yourself. Use this if you do need to know the file's name.

    use Fcntl;
    use POSIX qw(tmpnam);

    # try new temporary filenames until we get one that didn't already
    # exist;  the check should be unnecessary, but you can't be too careful
    do { $name = tmpnam() }
        until sysopen(FH, $name, O_RDWR|O_CREAT|O_EXCL);

    # install atexit-style handler so that when we exit or die,
    # we automatically delete this temporary file
    END { unlink($name) or die "Couldn't unlink $name : $!" }

    # now go on to use the file ...

If you're committed to doing this by hand, use the process ID and/or the current time-value. If you need to have many temporary files in one process, use a counter:

    BEGIN {
        use Fcntl;
        my $temp_dir = -d '/tmp' ? '/tmp' : $ENV{TMP} || $ENV{TEMP};
        my $base_name = sprintf("%s/%d-%d-0000", $temp_dir, $$, time());
        sub temp_file {
            local *FH;
            my $count = 0;
            until (defined(fileno(FH)) || $count++ > 100) {
                $base_name =~ s/-(\d+)$/"-" . (1 + $1)/e;
                sysopen(FH, $base_name, O_WRONLY|O_EXCL|O_CREAT);
            }
            if (defined(fileno(FH))
                return (*FH, $base_name);
            } else {
                return ();
            }
        }
    }

Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
[1nickt]: my $x = $aohoaoh->[0]->{' foo'}->[0]->{'bar' }; should work
[thepkd]: not indexing
[thepkd]: getting data out of the ds
[choroba]: it depends. Give more details, please
[thepkd]: i used a series of {}'s but it dont work
[choroba]: You need square brackets for arrays
[GotToBTru]: you can certainly construct a single expression to access any part of the data structure, without using temp variables. but you might make it easier on yourself and any other poor soul who has to understand your code if you do
[thepkd]: to dereference i mean
LanX .oO( oh tempz, oh moretz)
[thepkd]: @GotToBTru Sure. But.

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (14)
As of 2016-12-06 13:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (104 votes). Check out past polls.