package MyParser;
use strict;

my %types = (
    '0' => 'Unknown type',
    '1' => 'Space delimited word (len=>2)',
);

sub new {
    my ($class) = @_;
    $class = ref($class) || $class;
    return bless( {}, $class );
}

sub start_parser {
    my ( $self, $rtxt, $limit ) = @_;
    if ( ref $rtxt eq 'SCALAR' ) {
        $self->{BUFFER} = $$rtxt;
    } else {
        die "Unsupported";
    }
}

sub get_word {
    my $self = shift;
    if ( $self->{BUFFER} =~ s/^[\s\S]*?([a-zA-Z0-9+-]{2,})// ) {
        return ( 1, $1 );
    } else {
        return ( 0, undef );
    }
}

sub end_parser {
    my $self = shift;
    $self->{BUFFER} = '';
}

sub type_description {
    my ( $self, $id ) = @_;
    return $types{$id};
}

sub alltypes {
    my @types = ();
    map { $types[$_] = $types{$_} } keys %types;
    return @types;
}

1;

package main;

# parse input from STDIN

my $p = MyParser->new();

my ( $type, $word );

while (<>) {
    $p->start_parser( \$_ );
    while ( ( ( $type, $word ) = $p->get_word ) && $type ) {
        print $p->type_description($type), ",word=$word\n";
    }
    $p->end_parser();
}

my @types = $p->alltypes;
print map { "$_ => $types[$_]\n"; } 0 .. $#types;
