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

Hello,

I'm trying to make this piece of code work on MS Windows 2003 Server (ActivePerl 5.8.8):

#! perl use File::Temp; $File::Temp::KEEP_ALL = 1; # Tempfile to store command output. my $tf = new File::Temp(); my $tfname = $tf->filename; print "$tfname"; #print $tf "HELLO"; system("date /t >>$tfname");

But it fails with this error:

The process cannot access the file because it is being used by another process.

I've tested the code on Linux and it works fine. Is module behaviour different from Win32 to linux? Am I missing something?

Thanks in advance

Replies are listed 'Best First'.
Re: File::Temp problems on Win32
by runrig (Abbot) on Jul 29, 2008 at 16:08 UTC
    In windows, (by default?) a file can only be opened by one process. The $tf that File::Temp returns is a filehandle opened for writing. Your system command starts another process which tries to write to the same file, but it's locked by the first process. On *nix, more than one process can write to a file at the same time.
      In windows, (by default?) a file can only be opened by one process.

      Um, no. The default for C programs (and Perl is a C program) is to allow other processes to read/write to a file we have open but to not allow a file that we have open to be renamed or deleted (when using open() as opposed to CreateFile() or other calls not typical of portable C programs).

      Perhaps File::Temp is locking the file. File locks in Win32 are always manditory while they default to advisory on Unix. Perhaps just ask File::Temp to not lock?

      - tye        

      Thanks a lot for the fast response

      Does anybody know if there is a workaround or alternative for this case?

        Just use the filehandle that you have opened:
        print $tf `date /t`;
Re: File::Temp problems on Win32
by BrowserUk (Patriarch) on Jul 29, 2008 at 18:17 UTC