http://www.perlmonks.org?node_id=62389

One of my assistants has been a assigned a homework (*gasp* I used the "H" word- quick --!) in which he deals with quines. A quine is simply a program that prints it own source. Now I say "simply", but coming up with one, even just a short one, is an exercise in itself. As Ken Thompson adeptly points out in Reflections on Trusting Trust(1984), "If you have never done this, I urge you to try it on your own. The discovery of how to do it is a revelation that far surpasses any benefit obtained by being told how to do it." (He also says, "...FORTRAN was the language of choice for the same reason that three-legged races are popular" but that's another story.)

I certainly did find out he was right. In his paper, he gives some unsatisfying examples and suddenly I had to know more...

Thus, I ended up at

The Quine Page

Why is this in the maximum font size you ask? Well, that's because you haven't clicked on it yet. I'll wait now while you do that...

Ok. Hi. Welcome back. Now that you've browsed through at least a few quines, you have a general idea of how they work. I'd say the easiest one to understand is

Author: Travis Emmit $s="7072696e742724733d22272e24732e225c223b5c6e222e7061636b22482a222c24 +733b0a"; print'$s="'.$s."\";\n".pack"H*",$s;
This quine has simply hexidecimalized its source and it prints it out. That's all fine and dandy until you get to
Author: Allan M. Due #!/usr/local/bin/perl -w use strict; $_ = q(s|(.*)|print "#!/usr/local/bin/perl -w\nuse strict;\n\$_ = q(",$1,");\n",$1,";\n"|e); s|(.*)|print "#!/usr/local/bin/perl -w\nuse strict;\n\$_ = q(",$1,");\n",$1,";\n"|e;
In his attempt to get it to work, it looks like it automatically turned in obfuscation. As you know, our very own camel code makes four camels out one harmless looking camel. This is not exactly a quine, but a quine may also do impressive stuff with its output. The general method to use is 'initially store the program in a string for later printing' (after you use that same code). It even looks like an eval or two may come in handy for these jokers.

Believe it or not, quines, which seem to have a chicken-egg relationship, are useful in the making of compilers since the compiler needs to read sequences of the code it was written in. The C compiler was written in C (refer to Thompson Presentation). Thompson elaborates by describing how bugs could be written into compilers to create potentially untraceable hacks for security breachs (the topic of his presentation).

That said, I hope I've captured your interest. Perl seems to be a premiere language for developing quines (along with those cool PostScript examples). Feel free to include some cool links or examples of quines below. Oh, and for Petruchio, I include the obligatory link to the JavaScript quine page.

AgentM Systems nor Nasca Enterprises nor Bone::Easy nor Macperl is responsible for the comments made by AgentM. Remember, you can build any logical system with NOR.