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.

4 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:

$a=q!print '$a=q*'.$a.'*;';$a=~s/\x2a/\x21/g;print $a;!;print '$a=q!'.$a.'!;';$a=~s/\x2a/\x21/g;print $a;

Oops. Just put this on one line:

$a=q!print '$a=q'.$a.';';$a=~s/\x2a/\x21/g;print $a;!;
print '$a=q!'.$a.'!;';
$a=~s/\x2a/\x21/g;
print $a;

Leave a comment

About Joel Berger

user-pic As I delve into the deeper Perl magic I like to share what I can.