in reply to Re^2: Scoping question - will file handle be closed?
in thread Scoping question - will file handle be closed?

Filehandles are global, irrespective of the scope in which they are referenced. To say the filehandle closes when the reference count reaches zero is incorrect.

Not quite right, as you know by now, but also not completely wrong. Filehandles are a limited resource, no matter what language you use to write your software. So it is generally a good idea to close filehandles if they are no longer needed.

Modern operating systems with a lot of memory generally allow many file handles, but still you can run out of filehandles:

>perl -Mstrict -w -E 'my @h; for my $i (1..5000) { open $h[$i],"<","/d +ev/null" or die "$i $!"; }' 1022 Too many open files at -e line 1.

(Linux 3.10.17 x64)

C:\>perl -Mstrict -w -E "my @h; for my $i (1..5000) { open $h[$i],'<', +'nul' or die qq[$i $!]; }" 2046 Too many open files at -e line 1.

(Windows 7 64 Bit)

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)