Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: Temporary file management in Perl -- is it possible?

by mbethke (Hermit)
on Apr 24, 2013 at 21:15 UTC ( [id://1030534]=note: print w/replies, xml ) Need Help??

in reply to Temporary file management in Perl -- is it possible?

If the timer is the only one you need in your program, the most lightweight way would probably be to use File::Temp's UNLINK mechanism (it's a good idea to use this for tempfile creation anyway):

$tmp = File::Temp->new(UNLINK => 1); $SIG{ALRM} = sub { undef $tmp }; alarm(600);

However, you must be sure not to use that tempfile after the timeout. If you can be sure of that, question is why you don't just remove the file when you're done with it?

Replies are listed 'Best First'.
Re^2: Temporary file management in Perl -- is it possible?
by taint (Chaplain) on Apr 24, 2013 at 23:43 UTC
    Greetings mbethke, and thank you for the reply.
    I looked at File::Temp. But if I'm not mistaken, it creates an (altho temporary) actual copy of the file.
    While this wouldn't be the "end of the earth" for me, these files are ~150Mb each.
    Copy time, and space seems less efficient than using a symlink. Which is why I chose that direction.
    Maybe kennethk's suggestion solves this. Then again, perhaps initiating a check at the beginning of
    this script, similar to:
    #!/bin/sh - find . -type f -name '*.tbz2' -maxdepth 1 -cmin '+24' | xargs rm exit
    would be nearly good enough.
    OK, the above is a shell script, and while I could "shell out" within Perl.
    I'm sure there must be a way do do the same whithin Perl. :)

    Thanks again for taking the time to respond!


    #!/usr/bin/perl -Tw
    use perl::always;
    my $perl_version = "5.12.4";
    print $perl_version;
      If you want to do a clean-up first, you could invoke system at the start of your script:
      system(q{find . -type f -name '*.tbz2' -maxdepth 1 -cmin '+24' | xargs + rm})
      You can also invoke all necessary commands in Perl
      opendir my $dh, '.'; for (readdir $dh) { unlink if -l and /\.tbz2$/ and 24 * 60 * -M > 10; }
      See -X.

      #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

        Greetings kennethk, and thank you for taking the time to provide this solution!
        That's exactly what I was thinking. But, while having read -X, still hadn't figured out how to accomplish it in Perl.
        Thanks, again. I really appreciate it.


        #!/usr/bin/perl -Tw
        use perl::always;
        my $perl_version = "5.12.4";
        print $perl_version;

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1030534]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2024-06-22 04:11 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.