Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: Unlink Permission Denied

by tachyon (Chancellor)
on May 03, 2002 at 04:09 UTC ( #163703=note: print w/replies, xml ) Need Help??

in reply to Unlink Permission Denied

The problem stems from the fact that under Win32 you can't delete a file that the OS regards as open. Here is an example.

$file = 'c:\test.txt'; open F, ">$file" or die "Can't open $file $!\n"; # uncomment this to gain permission to unlink $file... # close F; unlink $file or die "Can't unlink $file $!\n";

If you close the filehandle opened on $file you can unlink it, if not you get permission denied. When you run your script with an output redirect to a file the OS filehandle on that file is not closed until the shell has finished running your script, ergo you can't delete that redirect file until your script finishes but you need the script running to do the unlink. Catch 22. Solution: write and delete the file from within your script. This works fine:

$file = "c:\\test.txt"; `dir c: > $file`; unlink $file or die "Can't unlink $file $!\n";

I can't see any good reason to do it but you can do this on the command line:

perl > somefile.txt; del somefile.txt

PS the or $! == 2 makes no sense to me as $! is unlikely to ever contain a string that evals to 2




Replies are listed 'Best First'.
Re: Re: Unlink Permission Denied
by rob_au (Abbot) on May 03, 2002 at 05:52 UTC
    I can't see any good reason to do it but you can do this on the command line

    One place where this approach to files and file handles can be used is in the generation of temporary files - As discussed briefly in Using Temporary Files in Perl, where the underlying operating system allows this, temporary files can be unlinked while their file handle is held open. This results in an anonymous STDIO stream, which is excellent for the purposes of scoped temporal storage, side-stepping many of the race issues encountered in temporary file name generation and usage. The only disadvantage from this approach is that the data stored within the STDIO stream can only be accessed through the opened file handle.

    This method of temporary file handling is also discussed briefly with reference to Windows systems in the File::Temp documentation.


      When I said I could not see any good reason to do what seemed to being done I was refering to the external redirect and internal unlinking of the redirect file. I like the IO::File inplementation for temp files myself:

      require IO::File; $fh = new IO::File->new_tmpfile




      Well now.

      This is what I love about this place. Always something new to learn from the posts.

      Never used File::Temp or IO::File for the matter. Makes more sense to use these mods for what I was doing. It also didn't require much change for the code!

      Thanks you and ++

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://163703]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (8)
As of 2018-11-12 20:22 GMT
Find Nodes?
    Voting Booth?
    My code is most likely broken because:

    Results (144 votes). Check out past polls.