Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Temporary file management in Perl -- is it possible?

by taint (Chaplain)
on Apr 24, 2013 at 20:12 UTC ( #1030511=perlquestion: print w/ replies, xml ) Need Help??
taint has asked for the wisdom of the Perl Monks concerning the following question:

Greetings, I'm trying to discover if I can give a temporary file a "Life Span"
in Perl. Is such a thing even possible? My sutuation:
I'm generating a symlink (perlfunc=>symlink) that prefaces a semi-random
number to the $filehandle, thusly:
#!/usr/bin/perl -wT # testing with a web browser print "Content-type: text/html\n\n"; use strict; use POSIX qw(strftime); my $gmtstring = strftime "%H-%I-%S", localtime; my $filehandle = "relative/path/to/filename"; my $dir = "." if (-r $filehandle) { $filehandle = "readable"; }else{ $filehandle = "unreadable"; } if (-w $dir) { $dir = "writable"; }else{ $dir = "unwritable"; } unless ($filehandle && $dir) { symlink("$filehandle", "$gmtstring-$filehandle") }else{ print "$filehandle<br />$dir"; }
This much returns the anticipated/expected results. However,
I want the newly created symlink to "vanish" (perlfunc=>unlink)
after --say, a 10 minute period. Is there ANY way to tell Perl to unlink
the symlink -- like creating a timer, or something? I'm not sure where to go with
this. I could create a cronjob. But I would really rather keep
this "self-contained" if it's even remotely possible.

Thank you for all your time, and consideration.

--chris

UPDATE It also occurred to me that a session || sessions, might also be a solution.
In the "big picture" these "temporary" symlink(s), are largish files of complete systems for
embedded systems. I only want them to become available for those that actually want to use them --
not to embellish other ppl's web pages, and for "bots" to suck down ~50 times/day. So
it occurs to me that the "Life Span" of the file could be tied to a session.
Is that possible, or a better approach?

Thanks again.

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

Comment on Temporary file management in Perl -- is it possible?
Download Code
Re: Temporary file management in Perl -- is it possible?
by kennethk (Monsignor) on Apr 24, 2013 at 20:23 UTC
    I'd say a cron job that removes files older than 10 minutes is in general cleaner, but one possibility would be fork and wait 10 minutes, a la:
    if (!fork) { sleep 600; unlink "$gmtstring-$filehandle"; exit; }

    Of course, for production, you'll want to test to make sure fork returns defined, probably set $SIG{CHLD}='IGNORE' (see perlipc) and a couple other clean-ups.


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

Re: Temporary file management in Perl -- is it possible?
by rnewsham (Hermit) on Apr 24, 2013 at 20:59 UTC

    Perhaps Schedule::At will do what you want. I have used the at command for similar tasks. I have not used this module so don't have any example code but it looks like it should work.

Re: Temporary file management in Perl -- is it possible?
by pvaldes (Chaplain) on Apr 24, 2013 at 21:08 UTC
    I'm trying to discover if I can give a temporary file a "Life Span"

    Yes, in pseudocode:

    open file line (creates the file). Do something and close the file

    sleep for 10 minutes and unlink the file

    see also File::Temp.
Re: Temporary file management in Perl -- is it possible?
by mbethke (Hermit) on Apr 24, 2013 at 21:15 UTC

    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?

      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!

      --chris

      #!/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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1030511]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (13)
As of 2014-11-28 14:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (197 votes), past polls