PPI highlighted Mojolicious "quine"
On February 28th I will be presenting an “Introduction to Mojolicious” to the Chicago.pm meeting. If you are in the area I hope you will stop by; it won’t just be an introduction despite the name. If you are interested, here is the Meetup link.
I haven’t decided, but I might try to “self-host” the talk, writing it as a Mojolcious app! To do that I had to resurrect one of my earliest CPAN releases Mojolicious::Plugin::PPI. This module does just what the name should imply, providing syntax highlighting via PPI and PPI::HTML in a handy Mojolicious plugin.
Whether or not I decide to use this for my talk, it still is handy to have around. Here is a cute example, a simple “quine” which you can run:
#!/usr/bin/env perl
use Mojolicious::Lite;
plugin 'PPI' => { toggle_button => 1 };
get '/' => sub {
my $self = shift;
$self->stash( file => __FILE__ );
$self->render('quine');
};
app->start;
__DATA__
@@ quine.html.ep
% title 'A Mojolicious "Quine"';
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
%= javascript 'ppi.js'
%= stylesheet 'ppi.css'
</head>
<body>
<h2><%= title %></h2>
%= ppi $file
</body>
</html>
A quine is a program that prints its source code as its output. In truth, this “quine” is really a cheater, a true quine doesn’t read itself. Still, the above example renders the source code nicely highlighted by PPI (and with some added goodies like toggleable line numbers).
Just for fun, some other Perl (cheater) quines are:
@ARGV = $0; print for <>
and
seek DATA,0,0; print for <DATA>
__DATA__
But those outputs aren’t syntax highlighted. For a real one see the comments.
This is very cool but I have to be out of town for it. Maybe I should prepare a Mojo::UserAgent talk. :)
Just a note: For a program to be a quine, it should accept no input. Reading its own source code disqualifies your examples as quines. This is a Perl quine, though:
Oops. Just put this on one line:
Ok fine, I thought that that must be too easy :-).
Anyway the main example above isn’t even really that, I give it a title and a heading, the line numbers are added and the toggle button springs into existence. Further, I’m not showing the source of Mojolicious nor my plugin.
Its for those reasons that it used “quine” in quotes (hmmm, I missed the title). You are right though I should remove the others, they cheat.