Welcome to WebmasterWorld Guest from 54.162.226.212

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

Closing file handles

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

5+ Year Member



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 Jul 23, 2008 (gmt 0)

5+ Year Member



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 Jul 23, 2008 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



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 Jul 23, 2008 (gmt 0)

5+ Year Member



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

WebmasterWorld Senior Member 5+ Year Member



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 Jul 24, 2008 (gmt 0)

5+ Year Member



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

Featured Threads

Hot Threads This Week

Hot Threads This Month