Gentoo::ChangeLog::Parser::Eventual - Rudimentary Event-Based ChangeLog format parser, inspired by Pod::Eventual.
version 0.1.2
use Gentoo::ChangeLog::Parser::Eventual my $parser = Gentoo::ChangeLog::Parser::Eventual->new( callback => sub { my ( $parser, $event, $opts ) = @_ ; }, ); $parser->handle_line( "This is a line", { key => 'value', line => 1 });
In the proceeds of making a ChangeLog parser, I kept getting stuck on various parts with writing it cleanly.
This design, inspired by RJBS' Great Pod::Eventual, greatly simplifies the process by using very rudimentary and loose data validation.
Pod::Eventual
Lines are fed in manually, because we didn't want to implement all the File IO our self and didn't want to limit the interface by forcing passing a file handle.
You can do the IO quite simply anyway.
while( my $line = <$fh> ){ chomp $line; $parser->handle_line( $line , { line => $. } ); }
A parser instance has a bit of state persistence, so you should use only 1 parser per input file.
Currently, it can only detect a few basic things.
We go naive and classify that entire "# ChangeLog for " section at the top of a ChangeLog as a "Header".
The header itself is not validated or parsed in any way beyond the notion that its a series of comments.
Raises an event when it sees
*perl-5.12.2 (10 Jun 2010)
This is the part on the top of each ChangeLog entry as follows:
10 Jun 2010; Bob Smith <asd>:
There are multiple ways this can be done however, so there are 3 events for this.
This is the part after the header.
handle_line is the only public method on this object. It takes one line, processes its own state a bit, works out what event(s) need to be thrown, and call the passed callback.
This must be a string, and this is the string that represents a singular line from the ChangeLog to be parsed. This code is written under the assumption that you have also pre-chomped all your lines, but doesn't enforce it. However, its not guaranteed to work, and is not tested for, and may in a future revision be enforced.
This is a HashRef of data to be sent through to the event handler.
This is a good place to specify the source line number of the line you are currently parsing if you want that.
$object->handle_line("this line", { line => 4 } );
and then in the callback:
my( $parser, $event, $opts ) = @_ ; print $opts->{line} = 4;
Outside construction and providing this (required) attribute, no public methods exist for working with it.
my $object = ::Elemental->new( callback => sub { my( $parser, $event, $opts ) = @_ ; .... event handler code here .... });
This is the name of the event that has been triggered. See "EVENTS".
This is a Hash Reference of data about the event. Mostly, it contains whatever data was passed from "handle_line", but it injects its own 'content' key containing a copy of the string that was parsed.
You can manually execute the CodeRef as if it were called internally, but there is little point to this.
$object->handle_event( 'an-event-name' => { } );
Note, that the event-names list is baked into this class, and manually calling this method and passing an unsupported event name will result in casualties.
Fires when the first line is parsed.
Fires on blank ( i.e.: all white space ) lines.
Fires on the first header line.
Fires on all comments that are deemed "part of the header"
Fires on the first line that is obviously not part of the header, terminating the header.
Fires on *perl-5.12.2 lines.
*perl-5.12.2
Fires on Single-line change headers.
Fires on each line that looks like it was a child of the previous change header.
Fires when the first line is seen that indicates the change body is complete.
Fires on the first line of a multi-line change header.
Fires on all non-blank lines in a multi-line change header other than the first and last.
Fires on the last line of a multi-line change header
Fires in the event no processing rules indicated a success state.
Kent Fredric <kentnl@cpan.org>
This software is copyright (c) 2013 by Kent Fredric <kentnl@cpan.org>.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Gentoo::ChangeLog::Parser::Eventual, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Gentoo::ChangeLog::Parser::Eventual
CPAN shell
perl -MCPAN -e shell install Gentoo::ChangeLog::Parser::Eventual
For more information on module installation, please visit the detailed CPAN module installation guide.