Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: script fails with perl 5.004_02

by jwkrahn (Abbot)
on Oct 27, 2019 at 21:39 UTC ( [id://11108025]=note: print w/replies, xml ) Need Help??


in reply to script fails with perl 5.004_02

It seems that the old perl interpreter has an issue with opendir($DIR, $PATH); If I try to initialize the $DIR variable as $DIR = 0; then it stops working when the first directory is encountered. If I try opendir(DIR, $PATH) then again, it works as long as I don't go into a sub-directory. Then it loses track.. So, how did people overcome this issue before any newer versions of Perl existed?
my $DIR; my $FULLNAME; opendir($DIR, $PATH) or return;

Back then you had to use local with a typeglob:

local *DIR; my $FULLNAME; opendir(DIR, $PATH) or return;

Replies are listed 'Best First'.
Re^2: script fails with perl 5.004_02
by afoken (Chancellor) on Oct 28, 2019 at 05:55 UTC
    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". ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11108025]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-04-24 00:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found