I have written a parsing module that should be able to handle very large files containing database dumps etc. Some files can reach the size of up to 1 or 2 Gig so it is important to parse the file by part instead of trying to load it into memory.

After running the program it appears to me that my program is getting stuck in an infinite loop and restarting around number 384000. Either my program is re-starting and parsing the same file again (which I doubt since I delete the file on completion), or it is somehow choking and re-starting the parse from the file's beginning

The following are the pertinent pieces of code. Am I going about this parse in the wrong way?

The following is some of the module code
my @articles = (); my @titles = (); my $reader = new IO::Handle; my $done_flag = 0; my $total = 0; sub new{ my $class = shift; my $self = bless{}; return $self; } sub parseFile{ my ($self, $file) = @_; if(open(READ, $file)){ if(!$reader->fdopen(fileno(READ), "r")){ die "Cannot open file [$file] for reading\n"; } }else{ die "Cannot open file [$file] for reading\n"; } loadContents(); } sub loadContents{ my $count = 0; while((my $line = $reader->getline) && $count < 3000){ if(($line =~ /^\d/) && !($line =~ /\Q#REDIRECT\E/)){ #new valid record my @data = split /\s+-separator-\s+/, $line; push @articles, $data[2]; push @titles, $data[1]; $count++; $total++; } } if($count < 3000){ $done_flag = 1; } #close(READ); } sub closeParser{ $reader->close; } sub getArticle{ if(scalar(@articles) > 0){ my $article = pop @articles; my $title = pop @titles; return parseData($article, $title); }else{ return (); } } sub hasArticles{ if(scalar(@articles) == 0 && $done_flag){ return 0; }elsif(scalar(@articles) == 0 && !$done_flag){ loadContents(); return 1; }else{ return 1; } }
From my main program the following is used to call the parser and run through the entire file.
my $parser = new Parser(); $parser->parseFile($feed_location$file); while($parser->hasArticles()){ my ($url, $author, $source, $date, $title, $body) = $parse +r->getArticle(); #database logic taking place here. } $parser->closeParser(); unlink($feed_location$file);
Thanks for any help!

In reply to Parsing large files by Grundle

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.