Re: Deleting files



Bart Lateur wrote:

Anders F wrote:

Is there a Perl subroutine for deleting files?

There's a builtin. It's called "unlink".

http://perldoc.perl.org/functions/unlink.html

The reason for the odd name is because on Unix, there's no garantee that
if you succesfully apply this function on a file, that it will indeed be
deleted. There' something called a "hard link", so there can be multiple
directory entries, possibly in more than one directory, for the same
file. "unlink" will remove one such directory entry (called a "link").

Only when all links are gone, will the file contents indeed be deleted.

And indeed, when the final handle to the file is closed:

eg:

open F, ">Somefile";
unlink "Somefile";

print F "Wibble\n"; # File data exists, but there is no way to re-open
# it as the directory entry has gone. However, F may be cloned or passed to
# a sub process via fork().

close F; # Now the file goes away

Cheers

Tim
.



Relevant Pages

  • Re: Deleting files
    ... The reason for the odd name is because on Unix, there's no garantee that ... There' something called a "hard link", ... directory entries, possibly in more than one directory, for the same ...
    (comp.lang.perl.misc)
  • Re: [SLE] preserving files creation time
    ... I'm not sure if I can provide the answer directly, but a Linux (and Unix) ... create an additional hard link to a file, ctime will change. ... This is a hard link to the current directory. ...
    (SuSE)
  • Re: Hard link
    ... Ok if the date in secret.txt was "My name is Michael" the same data wan in ... Or create a hard link with Unix, ... Dos and ...
    (microsoft.public.windowsxp.help_and_support)
  • Re: Ada.Directories
    ... On a Unix system it _is_ a hard link. ... the interface contract (AFAIK even described in various standards). ...
    (comp.lang.ada)