[Israel.pm] Open files in recursion

Berler Chanan chananb at centerity.com
Wed Apr 22 06:38:26 PDT 2009


Great it, works now....
Never knew I can do that....
New things every day :-)
Thanks
Chanan

-----Original Message-----
From: perl-bounces at perl.org.il [mailto:perl-bounces at perl.org.il] On Behalf Of Gaal Yahas
Sent: Wednesday, April 22, 2009 2:37 PM
To: Perl in Israel
Subject: Re: [Israel.pm] Open files in recursion

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/
_______________________________________________
Perl mailing list
Perl at perl.org.il
http://mail.perl.org.il/mailman/listinfo/perl
No virus found in this incoming message.
Checked by AVG - www.avg.com 
Version: 8.0.238 / Virus Database: 270.11.58/2061 - Release Date: 04/21/09 16:48:00



More information about the Perl mailing list