use strict; use warnings; package Parser::RKG; use Parse::RecDescent; use Data::Dumper; use RegExp::Common qw(URI); use constant DEBUG => 0; $::RD_HINT = 1 if DEBUG; $::RD_TRACE = 1 if DEBUG; my $g = q( CommentLine: '#' /.*/ {{comment=>$item[2]}} UriLine: 'u=' /$RE{URI}{HTTP}/ {{uri => $item[2]}} Line: CommentLine | UriLine {$item[1]} ); sub new { my ($class) = @_; my $self = {}; bless ($self, $class); $self->{parser} = Parse::RecDescent->new($g); $self->{parse_results} = []; return $self; } sub parse_line { my ($self, $line) = @_; chomp $line; return 1 unless defined($line); # empty is a valid do-nothing return 1 unless $line =~ /\S/; # empty is a valid do-nothing print "parsing: $line\n" if DEBUG; my $pat = $self->{parser}->Line($line); return 0 unless $pat; # failure push ( @{$self->{parse_results}}, $pat ); return 1; # success } sub parse_results { my ($self) = @_; return $self->{parse_results}; } 1;