Back then you had to use local with a typeglob:
But either way, recursing directories while keeping their handles open will eventually run out of file handles.
Demo, repeatedly opening the current directory just to use up all available handles:
#!/usr/bin/perl
use strict;
use warnings;
sub openHandle
{
opendir my $dir,'.' or die "Could not opendir: $!";
return $dir;
}
my @a;
while (1) {
push @a,openHandle();
print "Handles in use: ",0+@a,"\n";
}
Running on Linux 64 bit, using perl 5.22.2:
// previous lines removed
Handles in use: 1015
Handles in use: 1016
Handles in use: 1017
Handles in use: 1018
Handles in use: 1019
Handles in use: 1020
Handles in use: 1021
Could not opendir: Too many open files at handles.pl line 8.
Add three handles for STDIN, STDOUT, STDERR, and you get a total of 1024 handles. Note that no other files, directories, or network connections are currently open.
Running on Windows 7 64 bit, Strawberry Perl 5.14.2 64 bit:
// previous lines removed
Handles in use: 27781
Handles in use: 27782
Handles in use: 27783
Handles in use: 27784
Handles in use: 27785
Terminating on signal SIGINT(2)
H:\tmp>
It takes several seconds to clean up the mess after pressing Ctrl-C.
According to MS Technet, Windows can open about 16.7 million handles.
MSDOS, in its default configuration, had a file handle limit of just 8, including STDIN, STDOUT, STDERR, plus two extra default handles for a communication port and a printer port. This left software with just three handles. Using the FILES directive in config.sys, you could increase that to 255 handles, a typical value was 20 or 30.
If you want to play save, close handles as soon as possible. For recursing directories, read the entire content, close the handle, and only then, process the content. Of course, this may need more memory.
Alexander
--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
|