Welcome to WebmasterWorld Guest from 54.145.235.23

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

Closing file handles

     
3:21 pm on Jul 23, 2008 (gmt 0)

New User

5+ Year Member

joined:July 17, 2008
posts:30
votes: 0


im having a with file handles is there a way to see if there are any file handles that haven't been closed

Also is there a way to see if there are closes with out any opens

an example is in my script i have

close OUT;

but there is no open (OUT, '<file');

any one who can help thanks

4:53 pm on July 23, 2008 (gmt 0)

Preferred Member

10+ Year Member

joined:Jan 5, 2006
posts:536
votes: 0


if you use the warnings pragma:

use warnings;

it will warn you of things like OUT only being used once in the script. You also need to get into the habit of checking the success/failure of the system calls:

close(OUT) or die "Can't close OUT: $!";

or:

close(OUT) or warn "Can't close OUT: $!";

same when you open() files or do any number of things.

4:56 pm on July 23, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:May 31, 2008
posts:661
votes: 0


you can also use variables as filehandles, which gives you all the fun of variables. use a hash, iterate over it and close and delete any key you find.
7:42 pm on July 23, 2008 (gmt 0)

New User

5+ Year Member

joined:July 17, 2008
posts:30
votes: 0


ok thanks but what a hash
8:09 pm on July 23, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:May 31, 2008
posts:661
votes: 0


a hash is like an array, only it is not sorted and it's keys are strings (you can use numbers but they're treated as strings)

my %hash = ();

open($hash{'filehandle1'}, '>', 'test.txt') ¦¦ die $!;
print {$hash{'filehandle1'}} ("hello");

for my $key (keys %hash)
{
close($hash{$key}); # all filehandles are closed now
}

but of course, you should keep track of your filehandles anyhow, this is just a workaround.

4:10 pm on July 24, 2008 (gmt 0)

New User

5+ Year Member

joined:July 17, 2008
posts:30
votes: 0


ok thanks for the advice for the warnings it helped me with some of my files handles that weren't closed.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members