Grundle has asked for the wisdom of the Perl Monks concerning the following question:
From my main program the following is used to call the parser and run through the entire file.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; } }
Thanks for any help!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);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Parsing large files
by tilly (Archbishop) on Apr 10, 2005 at 19:23 UTC | |
by Grundle (Scribe) on Apr 10, 2005 at 20:59 UTC | |
by Joost (Canon) on Apr 10, 2005 at 21:07 UTC | |
by tilly (Archbishop) on Apr 10, 2005 at 21:16 UTC |