[sf-perl] A basic text parser

Balaji balaji.ramasubramanian at gmail.com
Fri Feb 8 21:37:29 PST 2019


Hi all,

I've been writing Perl for several years and I've always wondered that
while text parsing is the most common thing done by all Perl programmers,
why is there no lean and clean way to do it. Every time one has to write
code like this:

open FH, 'filename';

while (<FH>) {
   if (m//) {
      ## Some condition
   } elsif(m//) {
      ## Another condition
   } else {
      ## More
   }
}

close FH;

This is unfortunate. So I thought I'd make it a little simpler. I've posted
a CPAN package named Text::Parser <https://metacpan.org/pod/Text::Parser>.
It is a generic parser and you can very easily extend it to parse your own
text formats by making a derived class, overriding just one method. If you
do so, your main code could look this lean and mean:

use MyComplexTextFormatParser;

my $parser = MyComplexTextFormatParser->new();
$parser->read('filename.complex_format');
my (@records) = $parser->get_records();

Try it out and feel free to give feedback or file issues on the GitHub
repository <https://github.com/balajirama/Text-Parser> page.

Thanks,
Balaji
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.pm.org/pipermail/sanfrancisco-pm/attachments/20190208/cae7cc12/attachment.html>


More information about the SanFrancisco-pm mailing list