in reply to Perl File Handle Count not working as expected, why?

Filehandles get closed automatically. For example when you open a new filehandle in $fh, the old filehandle gets closed automatically. See open and/or close, I guess. Also, why would you want to use sysopen instead of plain open?

Replies are listed 'Best First'.
Re^2: Perl File Handle Count not working as expected, why?
by radnus (Novice) on Jul 14, 2010 at 18:11 UTC
    Using plain open, produces the same effect. That is no, close, but still the file handle count remains the same. From the literature, I gather that only IO::File does auto close. I am confused.....Please help.... use strict; my $i = 0; # Get the current Process Id my $proc = getpgrp(0); # Create 10 files.. while ($i < 10) { $i++; # Get a new File Handle # Count the File Handle for this process... system ("ls -l /proc/$proc/fd/ | wc -l"); # Open a new File open (FH, "+>", "/tmp/$i") or die "$!"; # Write something to the file print FH "stuff \n"; #increment counter, to create a newer file. ## NO CLOSE... #print "COunter $i\n"; } 1;

      [ Please use <c>...</c> tags around your code ]

      Switching from a lexical to a global variable changes nothing. You're still overwriting (and thus closing) the previous pass's file handle every time you go through the loop.

      You need to store the the handle somewhere it doesn't get overwritten, such as in an array.