Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Perl File Handle Count not working as expected, why?

by radnus (Novice)
on Jul 14, 2010 at 17:37 UTC ( [id://849582]=perlquestion: print w/replies, xml ) Need Help??

radnus has asked for the wisdom of the Perl Monks concerning the following question:

#!/perl/me -w use strict; use FileHandle; my $i = 0; # Get the current Process Id my $proc = getpgrp(0); # Create 10 files.. while ($i < 10) { # Get a new File Handle my $fh = new FileHandle(); # Count the File Handle for this process... system ("ls -l /proc/$proc/fd/ | wc -l"); # Open a new File sysopen ($fh, "/tmp/$i", O_RDWR) or die "$!"; # Write something to the file print $fh "stuff \n"; #increment counter, to create a newer file. $i++; ## NO CLOSE... }
/tmp> ./perl junk2.pl 5 5 5 5 5 5 5 5 5 5
I was expecting it to go UP by 1 each time in the loop, but it stayed at 5. Why?

20100721 Janitored by Corion: Added formatting, code tags, as per Writeup Formatting Tips

Replies are listed 'Best First'.
Re: Perl File Handle Count not working as expected, why?
by Corion (Patriarch) on Jul 14, 2010 at 17:50 UTC

    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?

      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.

Re: Perl File Handle Count not working as expected, why?
by jwkrahn (Abbot) on Jul 14, 2010 at 17:57 UTC
    I was expecting it to go UP by 1 each time in the loop, but it stayed at 5. Why?

    Because getpgrp returns the current process group.    You want the current Process Id which is in the $$ variable.

      Although correct, this is not the issue here (the process group id and the process id are numerically the same in this case).

      The real issue is that by creating a new $fh with

      my $fh = new FileHandle();

      the old handle will go out of scope, so the file is automatically being closed.

      If you store away $fh somewhere, e.g.

      my $fh = new FileHandle(); push @fhandles, $fh; # keep it from going out of scope ...

      the file handle count will go up.

        THANKS!!!!
Re: Perl File Handle Count not working as expected, why?
by TedPride (Priest) on Jul 15, 2010 at 05:27 UTC
    For anyone too lazy to view source on the page, here's his original post with code tags:
    #!/perl/me -w use strict; use FileHandle; my $i = 0; # Get the current Process Id my $proc = getpgrp(0); # Create 10 files.. while ($i &lt; 10) { # Get a new File Handle my $fh = new FileHandle(); # Count the File Handle for this process... system ("ls -l /proc/$proc/fd/ | wc -l"); # Open a new File sysopen ($fh, "/tmp/$i", O_RDWR) or die "$!"; # Write something to the file print $fh "stuff \n"; #increment counter, to create a newer file. $i++; ## NO CLOSE... } /tmp> ./perl junk2.pl 5 5 5 5 5 5 5 5 5 5
    I was expecting it to go UP by 1 each time in the loop, but it stayed at 5. Why?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-04-19 16:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found