=pod
... do not be alarmed.
What happened was that I was
talking on Reddit with Masklinn about
Literate Programming, and s/he wanted to know if there were any systems other than Literate Haskell which allowed for
blog posts which are valid code. "Hrmmm," I thought, "I don't see any obstacle to
doing that with
POD..."
[Yes, my inner monologue does contain hyperlinks. :-) ]
Literate Programming, by the way, is a workaround for the limitations of early Pascal compilers encountered by Donald Knuth when he was writing TeX. The bit everyone remembers is about intermingling code with a description of that code - a program called weave would then produce TeX source, which could be compiled into a beautifully-typeset manual, and another program called tangle would produce highly portable, lowest-common-denominator Pascal source, which could then be compiled into a runnable program. The original WEB system had many other features to enable higher-level programming without compromising portability: most of these are IMHO considerably less necessary with modern languages, and many of the rest are handled by systems such as Doxygen. Anyway, Literate Haskell handles the "intermingling code with docs" bit for Haskell, and POD does a similar job for Perl. Mark Dominus has written an
article explaining where POD falls short of Knuth's WEB system (and of Knuth's conception of Literate Programming) - as far as I can see, most of it applies to Literate Haskell too.
Knuth's original paper is, well, written by Donald Knuth - obviously it's worth reading! :-)
Anyway, having fulfilled Criterion 1 of Literate Programming (start with a long paragraph explaining what LP is and what it's for, and only then reveal that - shock, horror! The document you're reading is in fact a program!), I should get on with some actual coding.
=cut
print "Hello, Literate World!\n";
=pod
Did'ja see that? I totally embedded some code right in my blog post! Wow. Note that this was the classic "Hello, World" program - by tradition (which in hacker circles has a force greater than physical law and only slightly less than mathematical inevitability), this is the first program you must write with any new system.
Let's do something a bit more interesting:
=cut
while (<>) {
s/!+/./g; print;
}
=pod
That accepts some text on standard input (and/or any files specified on the command line), and replaces all sequences of exclamation marks with a single full stop. The result is then placed on standard output. I used to have that as a filter on my incoming mail - we had a very excitable JCR committee, who used to send out lots of emails of the form "Massive event happening tonight!!!!!!! Everyone must come!!!!!".
But, uh-oh, there's a problem - see where it says while (<>)? That's actually while (<>), which isn't valid Perl. But if I didn't do that, your browser would think I'd included an empty tag, which would disappear silently when your browser tried to render it - not very helpful. The upshot is that you can't just save this HTML file as witteringmoron.pl and expect it to work - you'll have to cut-and-paste the body of the post (being sure to get the =pod at the top) and save it separately. Sorry about that. But actually, you needed to do that anyway - the interpreter expects to start off in Perl mode, not POD mode, and all the HTML header gubbins isn't valid Perl.
Anyway, let's try it. Copy... paste into a new text editor window... save as witteringmoron.pl... now, open a command-line and type perl witteringmoron.pl:
Hello, Literate World! Excellent. Now, type some sample input:
This is, like, totally a test!!!!! ZOMG!!!!!!This produces the output
This is, like, totally a test. ZOMG.Which is correct, albeit somewhat deflating. You can stop the program from asking for more input by typing Ctrl-D.
So, yeah, you can write blog posts in POD, subject to the above limitations. Woo!
Right, back to work.