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

Hi Monks!

I need to delete a temp file created right after a link gets clicked, just can think how I could do that without having to submit all kinds of parameters back to the program, is there a way of doing that?
Any ideas, here is the code:

### Code to print page # I am reading from another file and parsing what I want betweeen thes +e tags if ($hold=~/<\?--printpg-->(.*?)<\/\?--printpg-->/gis) { my $page_to_print=$1; #generate freakyness here. my $freaky_number=int(rand(9999)) + 1; my $temp_file = "${curr_dir}".$freaky_number.".html"; # At this point I have the html page just the way I want, next +is to display it. open(FILE_IN, ">$temp_file") || warn "Can't open output file $t +emp_file: $!"; print FILE_IN "<body onLoad=\"self.print()\">"; print FILE_IN "$page_to_print"; close FILE_IN; # Here I am extracting just the file name out of the whole path if($temp_file =~/(.*?)\\([^\\]+)$/){ $file_hold=$2;} # Here I can send this page to the browser and it will open the + printer window automatically, it's done! print "<center><A HREF=\"javascript:void(0)\"onclick=\"window.o +pen('$file_hold','welcome','width=700,height=870')\">PRINT THIS PAGE< +/A></center>"; #But my problem is here, I need to get rid of the $temp_file fi +le, since I have to wait till the link to the #page to be printed is clicked I can't delete this file, I need + an idea on how to delete the file #right after the link gets clicked open(DEL_DATA, "$temp_file") || warn "Can't open output file $t +emp_file: $!"; undef $/; # Slurp mode $_ = <DEL_DATA>; my @temp_f=$_; my $tem_s=join("",@temp_f); close DEL_DATA; unlink($temp_file) || warn "Error removing $tem +p_file: $!"; }else{print "No match";}


Thanks for the Help!!!!

Replies are listed 'Best First'.
Re: Deleting a Temp File
by izut (Chaplain) on Aug 17, 2006 at 14:15 UTC

    First, you would use File::Temp for create your temporary file, instead writing your own algorithm. Second, you would rely on some user session data, for to associate which file is related to that client. Another approach I think would be nice is using (again) File::Temp with your own prefix - for example, /var/log/mytempfiles - and cron to remove old files in that directory every 10 minutes or so.

    Update: Corrected some english mistakes :-)

    Igor 'izut' Sutton
    your code, your rules.

      May be a way of deleting the previous created file by it's date in that directory, but how to get the date created of a file?

        I think you can use Unix find command for that. You can also use Perl for that using Find::File or File::Find::Rule. Check also -X and stat for more information on how get the creation date of some file.

        Igor 'izut' Sutton
        your code, your rules.

        Look at "perldoc -f -X", especially "-M" -- that reports the age of the file in days (a floating point number) relative to the time that the script started. (It's actually based on the date and time of "last modification" of the file, not "creation", but that shouldn't make any difference for you.)

        Also, File::Find (or anything related to it) is overkill if you just have one directory (no subdirectories) where the temp files are kept:

        # get a list of data files that are at least one day old in /my/tmpdir +: opendir( TMP, "/my/tmpdir" ); my @dayold = grep { /[^.]/ and -f and -M _ > 1 } readdir TMP; closedir TMP; # don't like old files? kill 'em off: unlink map { "/my/tmpdir/$_" } @dayold;
        If you want to focus on files that are, say, just 2 hours old or older, use "2/24" instead of "1" to test against the value of "-M".
Re: Deleting a Temp File
by graff (Chancellor) on Aug 18, 2006 at 04:28 UTC
    Um... if this is a cgi script that you are quoting from, it seems like it would make sense for the "PRINT THIS PAGE" link to point back to this same cgi script, with a specific parameter that means "send the printable data back to the client".

    In other words, you shouldn't need to create a temporary file at all (and therefore would not need to worry about how to delete the temp file). Add one more behavior mode to the current script, which will extract the "printable" content of your source data file (just like you're doing now), and print that to STDOUT (i.e. back to the client browser) instead of to a temp file.

    Decide on a cgi parameter that will activate this new mode, and put that (along with any other relevant params) into the href for the "PRINT" link, instead of a file name.