homepage Welcome to WebmasterWorld Guest from 54.226.93.128
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld
Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
Forum Library, Charter, Moderators: coopster & jatar k & phranque

Perl Server Side CGI Scripting Forum

    
Closing file handles
medranoenrique




msg:3705425
 3:21 pm on Jul 23, 2008 (gmt 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

 

perl_diver




msg:3705535
 4:53 pm on Jul 23, 2008 (gmt 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.

janharders




msg:3705537
 4:56 pm on Jul 23, 2008 (gmt 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.

medranoenrique




msg:3705656
 7:42 pm on Jul 23, 2008 (gmt 0)

ok thanks but what a hash

janharders




msg:3705672
 8:09 pm on Jul 23, 2008 (gmt 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.

medranoenrique




msg:3706435
 4:10 pm on Jul 24, 2008 (gmt 0)

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved