Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-04-19 23:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found