Click here to get back home

File Locked After Close?

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
File Locked After Close? xmp333 06-07-2008
Posted by xmp333 on June 7, 2008, 3:37 pm
Please log in for more thread options
Hi,

The last line of the following code snippet fails:

open FILE, '<file.dat' || die '...';
process(FILE);
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
file.

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

Thanks.

Posted by Dan Rumney on June 7, 2008, 4:10 pm
Please log in for more thread options
xmp333@yahoo.com wrote:
> Hi,
>
> The last line of the following code snippet fails:
>
> open FILE, '<file.dat' || die '...';
> process(FILE);
> close FILE;
> system('command file.dat');
>

[snip]

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

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?

Posted by Ilya Zakharevich on June 17, 2008, 12:31 am
Please log in for more thread options
[A complimentary Cc of this posting was sent to
Dan Rumney
> xmp333@yahoo.com wrote:
> > Hi,
> >
> > The last line of the following code snippet fails:
> >
> > open FILE, '<file.dat' || die '...';
> > process(FILE);
> > close FILE;
> > system('command file.dat');
> >
>
> [snip]
>
> > Any suggestions? I'm using ActiveState Perl on Windows running in a
> > DOS shell.
>
> Try checking the return code of the close function to see if there's
> some kind of problem preventing the file from closing

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

Hope this helps,
Ilya

Posted by Bill H on June 8, 2008, 6:46 am
Please log in for more thread options
On Jun 7, 3:37=A0pm, xmp...@yahoo.com wrote:
> Hi,
>
> The last line of the following code snippet fails:
>
> open FILE, '<file.dat' || die '...';
> process(FILE);
> 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. =A0In fact, if I open FILE on
> another file, the last line also works. =A0I'd like to fix the issue
> with something more elegant than re-opening the handle on a dummy
> file.
>
> Any suggestions? =A0I'm using ActiveState Perl on Windows running in a
> DOS shell.
>
> Thanks.

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

Posted by Dave Weaver on June 19, 2008, 3:11 am
Please log in for more thread options
> Hi,
>
> The last line of the following code snippet fails:
>
> open FILE, '<file.dat' || die '...';

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 '...';


Similar ThreadsPosted
FAQ 5.32: How do I close a file descriptor by number? December 16, 2004, 12:03 pm
FAQ 5.32 How do I close a file descriptor by number? February 6, 2005, 12:03 pm
FAQ 5.33 How do I close a file descriptor by number? May 5, 2005, 5:03 pm
FAQ 5.33 How do I close a file descriptor by number? July 21, 2005, 4:03 pm
FAQ 5.33 How do I close a file descriptor by number? October 7, 2005, 10:03 am
FAQ 5.33 How do I close a file descriptor by number? November 4, 2005, 5:03 pm
FAQ 5.33 How do I close a file descriptor by number? November 25, 2005, 11:03 pm
FAQ 5.33 How do I close a file descriptor by number? April 28, 2006, 9:03 am
FAQ 5.33 How do I close a file descriptor by number? August 20, 2006, 3:03 pm
FAQ 5.33 How do I close a file descriptor by number? November 13, 2006, 3:03 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap