[Israel.pm] Open files in recursion
Gaal Yahas
gaal at forum2.org
Wed Apr 22 05:37:18 PDT 2009
Instead of "DICT", use "my $dict".
Also, you might want to keep track of all the files you'd already
opened, to quickly find cyclic includes. Not strictly necessary
because you already have a depth guard, but useful if only to give
better error messages.
On Wed, Apr 22, 2009 at 4:30 PM, Berler Chanan <chananb at centerity.com> wrote:
> 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";
> }
>
> _______________________________________________
> Perl mailing list
> Perl at perl.org.il
> http://mail.perl.org.il/mailman/listinfo/perl
>
--
Gaal Yahas <gaal at forum2.org>
http://gaal.livejournal.com/
More information about the Perl
mailing list