http://qs1969.pair.com?node_id=741165


in reply to handler performance question

If the number of make/model pairs isn't likely to get too out-of-hand, you can also cache handles:
{ my (%handles, %did_mkdir); sub get_out_file { my ($make, $model) = @_; if (!defined $handles{$make}{$model}) { if (! $did_mkdir{$make} && ! -d "$out_dir/$make") { system "mkdir -p $out_dir/$make"; $did_mkdir{$make} = 1; } open $handles{$make}{$model}, "$out_dir/$make/$model" or die $!; } return $handles{$make}{$model}; } }
Then your main loop is reduced to:
while (<>) { my ($make, $model) = split /,/, $_, 4; my $out = get_out_file($make, $model); print $out $line; }
and all of the handles are closed at the end of execution. If this doesn't run you up against your filehandle limit, it will save you any number of unnecessary opens and mkdirs. If it does, well then you're going to have to start worrying about discarding entries from the cache, but you also have to ask yourself whether performance trumps complexity in that case. :)