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

I read in perdoc that there is no delete and only unlink can be done in perl.However am using the below code to delete a file,change the permissions and then delete the file,for some reason the file still exists,any idea?

if ( -e $file_name ) { chmod 0777, $file_name or die $!; unlink file_name; }

Replies are listed 'Best First'.
Re: Deleting or unlinking a file
by cdarke (Prior) on Mar 14, 2011 at 23:21 UTC
    In addition to the comments above, when a function does not appear to work then maybe you should consider checking the result to see why? Maybe you should always check?
    unlink $filename or die "Unable to unlink $filename: $!";
    By the way, it is called unlink because that is the C API name to delete a file on UNIX, and it is rather descriptive.
Re: Deleting or unlinking a file
by Eliya (Vicar) on Mar 14, 2011 at 23:11 UTC

    You omitted the '$' sigil on file_name.

    (use strict would've given you the hint 'Bareword "file_name" not allowed while "strict subs" in use'... )

      Actually that was a typo,I do have the "$" infront,what actually does unlink do?does it really delete the files in the directory?basically my script generates some "txt" files in between,so I am trying to delete them when I rerun the script,but seems like they are not getting deleted.

        It's called 'unlink' because it removes the directory entry (file name), which can be considered a 'link' to the actual file (the data).

        Note that (on Unix) multiple file names (hard or symbolic links) can point to the same file.  The unlink system call (or the user command of the same name) deletes the name, the file itself is only deleted if that name was the last (hard) link/reference to the file.

        "what actually does unlink do?"

        unlink explains what it does and how to check for failures.

        "what actually does unlink do?"

        The post by Eliya is very close to the mark. Since we are talking about "unlink", another point is of interest especially if you are ever writing a program that updates software.

        This discussion applies to Windows also! A "file" is a collection of data bits on the disk. The directory has a "human readable name" that "points" to those bits. The "open" translates the "human readable name" into a data structure that the file system uses to access the data bits, this is the "filehandle". Once a file is "open", the "name" doesn't matter <- this is important. If the file is open, you can change the name or even delete the name without affecting the data bits!

        Its been a while since I wrote a software updater for a continuously running system, but basically the idea is to copy all the important stuff onto the target. Say x.dll would get a temporary name of x_temp.dll. Then, x.dll gets "unlinked". Everyone who has this file "open" and is using it, continues to use it even though it has no "name". Then, x_temp.dll is renamed to x.dll and new requests for "x.dll" will get the updated file. The actual "old x.dll" file bits will get "deleted" when then are no longer in use (everyone using it does a close). "Delete" in this case means to mark that area of the disk as available for other use.

        From say the Windows command line, you might see something like >del X, unable to delete file X, file is in use. However the Perl unlink 'X' command will succeed.

        the above was just to round out discussion of "what does unlink" do - and it is a bit different than command line "delete". Sounds to me like the most likely thing in OP's scenario is wrong permissions on the directory. Unlink of a read only file is completely ok. Anyway, Perl "unlink" does what a UNIX person would expect, even on Windows.

Re: Deleting or unlinking a file
by marto (Cardinal) on Mar 14, 2011 at 23:21 UTC

    You seem to have forgotten the following after the shebang line:

    use strict; use warnings;

    See Use strict and warnings and Use strict warnings and diagnostics or die.

    "However am using the below code to delete a file,change the permissions and then delete the file,for some reason the file still exists,any idea?

    This doesn't make sense, you think you're deleting a file, changing permissions (of what?) then deleting the same file?

      I cannot delete it because the file is read-only mode,hence changing the perms

        Note that on Unix (which I suppose applies, because you tried chmod 777) a file doesn't have to be writable to be deletable.  The directory the file is in, however, must be writable.  This is because the directory index needs to be modified to remove the file name (=link).

        $ touch foo $ chmod 400 foo $ ls -l foo -r-------- 1 er er 0 2011-03-15 03:05 foo $ unlink foo $ ls -l foo ls: cannot access foo: No such file or directory

        So, as for your problem, is the directory writable?

Re: Deleting or unlinking a file
by GrandFather (Saint) on Mar 14, 2011 at 23:17 UTC

    Always use strictures (use strict; use warnings;). Try it. It will help!

    True laziness is hard work
Re: Deleting or unlinking a file
by TomDLux (Vicar) on Mar 15, 2011 at 01:50 UTC

    Do you have any prove that chmod and unlink were executed? Maybe $file_name has the wrong value, and so the '-e' test failed, and none of the if loop executed. I suggest expanding your test to:

    if ( -e $file_name ) { say "Deleting $file_name." chmod 0777, $file_name or die "Could not delete $file_name: $!; unlink $file_name; } else { say "In the else branch: '-e $file_name was false'."; }

    Update - added missing $$$$

    As Occam said: Entia non sunt multiplicanda praeter necessitatem.

      It is entering the if loop for sure,I can see a debug print statement I added getting printed but the actual file is still not getting deleted.