[Israel.pm] Open files in recursion

Berler Chanan chananb at centerity.com
Wed Apr 22 06:30:34 PDT 2009


Hi All,

Please take a look on this:
I am trying to open a dictionary file, and read the lines, when I found a
INCLUDE line, I need to open that file and read it too..
The idea to create a concatenated and long file with all the include
statements.

Problem: when doing the first open it works, and then it encounter with the
INCLUDE statement, and goes into recursion
In the recursion I need to open the new file again, but looks like DICT is
shared, and those it is closing the first file opened

Anyone has an idea how to bypass this ?
Thanks
Chanan

sub _create_dictionary_
{
    my ($dict_file, $depth) = @_;
    #our $buffer;
    
    if ($depth < 0)
    {
       # do not handle more than 5 nested INCLUDEs 
       return;
    }
    
    open DICT, $dict_file or die "Error: can't open $dict_file for
reading\n";
    while (my $line = <DICT>)
    {
        chomp($line);
	
	#$INCLUDE dictionary.compat
	if ($line =~ /^\$INCLUDE/)
	{
	   # handle include statment
	   my ($filename) = $line =~ /INCLUDE +(.*)/;
	   _create_dictionary_("dict/$filename", $depth - 1);
	}
	$buffer .= "$line\n";
    }
    close DICT or die "Error: can't close $dict_file after reading\n";
}



More information about the Perl mailing list