You're both right. Try a bit more in your sample.

#!/usr/bin/perl -w + # Strict use strict; use warnings; + # Main program print "Check file before unlink\n"; sleep 1; system("ls $0"); print "Unlinking the file $0\n"; sleep 1; unlink $0; print "Check file after unlink\n"; sleep 1; system("ls $0"); + print "File should be now be gone from disk (but still in memory)\n"; print "Try ^Z and check it with 'ls $0' ... (come back with 'fg')\n"; sleep 10; print while <DATA>; print "I'm still here, but the original file is long gone.\n"; __END__ foo
When you run this, even after the file is unlinked, the *DATA filehandle still works. That's because the file is still physically on the disk, even though it has been unlinked from the directory tree. Once the file is finally closed, the filesystem is allowed to reuse that diskspace. To prove this, I created a ram disk (thanks to a recent thread ;-}), and added a check using df to see the filesystem being used.
#!/usr/bin/perl -w + # Strict use strict; use warnings; use File::Basename; + # Main program print "Check file before unlink\n"; sleep 1; system("ls $0; df -k $0 +"); print "Unlinking the file $0\n"; sleep 1; unlink $0; print "Check file after unlink\n"; sleep 1; system("ls $0; df -k " +. dirname($0)); + print "File should be now be gone from disk (but still in memory)\n"; print "Try ^Z and check it with 'ls $0' ... (come back with 'fg')\n"; sleep 10; 1 while <DATA>; # ensure we use the *DATA filehandle in case perl opti +mises it away print "I'm still here, but the original file is long gone.\n"; __END__ foo
In this case I ram it and got output like this:
$ cp z.pl /ram; perl /ram/z.pl Check file before unlink /ram/z.pl Filesystem 1K-blocks Used Available Use% Mounted on none 100 4 96 4% /ram Unlinking the file /ram/z.pl Check file after unlink ls: /ram/z.pl: No such file or directory Filesystem 1K-blocks Used Available Use% Mounted on none 100 4 96 4% /ram File should be now be gone from disk (but still in memory) Try ^Z and check it with 'ls /ram/z.pl' ... (come back with 'fg') I'm still here, but the original file is long gone. $ df -k /ram Filesystem 1K-blocks Used Available Use% Mounted on none 100 0 100 0% /ram
Note how the file may have disappeared from /ram, but the disk usage wasn't actually freed up until I was done with the script.

I re-ran the script without the __END__ marker or the usage of *DATA, and found that the script is closed after compilation as the diskspace is freed immediately. Again, you're both right. :-)


In reply to Re^3: Deleting a file after it runs? by Tanktalus
in thread Deleting a file after it runs? by Spidy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.