Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
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.

In reply to Quine Whine by AgentM

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others contemplating the Monastery: (16)
    As of 2014-10-21 20:31 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      For retirement, I am banking on:










      Results (110 votes), past polls