in reply to Whats wrong in this code?

Add Error checking like use autodie qw' open unlink '; Close file Handle before try unlink

Replies are listed 'Best First'.
Re^2: Whats wrong in this code?
by shmem (Chancellor) on Nov 18, 2016 at 12:43 UTC

    Depends.

    qwurx [shmem] ~> touch foo.txt qwurx [shmem] ~> perl -Mautodie=open,unlink -le '$_=pop;open "<", $_; +unlink $_' foo.txt qwurx [shmem] ~> ls foo.txt ls: cannot access foo.txt: No such file or directory

    AFAIK...

    • Linux: Unlinking a file doesn't break the relation between an open file handle and the file system space allocated for the file; nor does holding an open file handle prevent unlink from succeeding, which is a directory related operation. The file's blocks are open to reallocation after the last reference to the file container is gone
    • Windows: Can't delete a busy file (the OS is aware of open file handles pointing to it)
    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

      Depends....

      Yes, there exist differences between the platforms

      And yes, the portable practice is preferable because it is portable

      And yes, error checking will let you know if there are errors