You are feel free to use the code I wrote at Why I like functional programming if you like. There are even pointers there on how to extend it to have substantially more sophisticated functionality than Everything does.

I would recommend a couple of minor changes though. Very specifically in the scrub_input function at the bottom the main loop should probably be:

while ($raw =~ /\G([\w ]*)/g) { $scrubbed .= $1; my $pos = pos($raw); if ($raw =~ /\G($is_handled)/g) { $scrubbed .= $handler->{ lc($1) }->(\$raw); # See perlre. If the handler matches something of # length 0, it won't match something of length 0 # the next time through. So... pos($raw) = pos($raw); } unless (pos($raw)) { if (length($raw) == $pos) { # EXIT HERE # return $scrubbed . $handler->{post}->(\$raw); } else { my $char = substr($raw, $pos, 1); pos($raw) = $pos + 1; $scrubbed .= &encode_entities($char); } } }
because at some point you are likely to want to have an optional handler for returns. Something like this:
sub { my $t_ref = shift; if ($$t_ref =~ /\G( *)/g) { my $indent = $1; $indent =~ s/ /&nbsp;/g; return "<br>\n$indent"; } else { confess("This shouldn't happen"); } }
That handler will preserve indents properly. (I say optional because some users do not like that kind of autoformatting, so it should be user-configurable.)

Anyways this node will probably make little sense to you without reading the other. Which may take a while to digest...


In reply to Re (tilly) 1: Unbalanced Tags by tilly
in thread Unbalanced Tags by lzcd

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.