What follows doesn't involve a lot of code, but demonstrates some nice things you can do with Perl's powerful. flexible, concise syntax. It is something I used in a project of mine where I am genearating a lot of C++ code.
The code generates a large number of source and header files. In its first incarnation, each file was just opened and closed as needed, like this:
open SRC "> file"; # print lots of stuff close SRC;
This was fine for a while, but as time went on the thing got slower and slower, as the number of files grew. Also the sounds coming out of the hard disc suggested that some optimisation was in order. Hmm.
So here's a wish list for some improvements;
Here's how it was done:
{ my %fhs; sub END { foreach (keys %fhs) { my $fh = $fhs{$_}; if ($_ =~ /\.h\.new$/) { print $fh "#endif\n"; } close $fh; if ($_ =~ /\.h\.new$/) { Clearcase::handleClearCaseElement($_); } } } sub openFile { my ($file, $header) = @_; unless (defined $fhs{$file}) { my $f; open ($f, "> $file") || die "could not open $file for output $!"; $fhs{$file} = $f; print $f $header; } return $fhs{$file}; } }
It's pretty simple. The END block handles cleanup, adds closing #ENDIFs where necessary, and calls some Clearcase handling routines which take care of version control.. The %fhs hash is private to the END and openFile methods. openFile takes an argument for header info for the file in my system. You could change this around to fit your requirements.
The other code has an easy time now. Whenever a certain file is wanted for output, just call my $fh = openFile('myFilename.cc', $header) and start printing to $fh. Job done.
The script is back down to a few seconds to run as a result of this (from over 30 seconds) and the disc is a lot happier. The code is much more readable too.
It would be wise, if the number of files gets very high, to extend this code to check that the number of open filehandles does not get too close to the system limit. It's possible to see some smart mechanism where least used filehandles are closed and only reopened when requested again ... as I don't need this right now, I'll leave this as an exercise for some future monk ...
In reply to handling multiple file handles for code generation by danmcb
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |