File Locked After Close?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

The last line of the following code snippet fails:

open FILE, '<file.dat' || die '...';
close FILE;
system('command file.dat');

It looks like FILE is still locked because if I break out the last
line into a separate script, it works.  In fact, if I open FILE on
another file, the last line also works.  I'd like to fix the issue
with something more elegant than re-opening the handle on a dummy

Any suggestions?  I'm using ActiveState Perl on Windows running in a
DOS shell.


Re: File Locked After Close? wrote:
Quoted text here. Click to load it


Quoted text here. Click to load it

Try checking the return code of the close function to see if there's
some kind of problem preventing the file from closing

If that doesn't show anything, can you provide the output from your program?

Re: File Locked After Close?

[A complimentary Cc of this posting was sent to
Dan Rumney
Quoted text here. Click to load it

Checking the return code of open() would not hurt either.  ;-)  :-(

Hope this helps,

Re: File Locked After Close?

On Jun 7, 3:37=A0pm, wrote:
Quoted text here. Click to load it

I believe this is more a function of Windows. I have had similiar
experiences with other software. If I open a in some programs just to
view it I find that I can't u/l it to a server with an ftp program
cause it is locked. When I close it I can.

I think the same thing is happening here, when you do the "system"
command windows is starting a new instance of command this new
instance cant access the file cause your original instance is "using"
the file.

If your perl code isn't doing anything after you make file.dat maybe
you could have an initial batch file that runs the perl script then
runs the file.dat pert after perl has exited.

Bill H

Re: File Locked After Close?

Quoted text here. Click to load it

The first line of your code snippet could be failing, and you'd not know it -
That is not checking the status of the open() call.

Due to operator precendence, that line is equivalent to:

   open( FILE, ('<file.dat' || die '...') );

Use the lower precedence 'or' operator, or parentheses:

   open( FILE, '<file.dat' ) || die '...';
   open FILE, '<file.dat' or die '...';

Personally, I'd  use a lexical file handle and the 3-arg version of open:

   open my $file, '<', 'file.dat' or die '...';

Site Timeline