What you've got there is a very basic quine, according to the recursion theorem (which for you listeners is the theoretical basis for a lot of cool stuff).

An interesting extension of that theorem, which makes fun use of perl's eval operator, is to build an arbitrarily complex quine with the same simple structure as outlined in the recursion theorem. The difference, though, is that the recursion theorem is based on the idea of breaking up your program into two pieces, each of which know how to print the other half. This basically involves duplicating all of your code, because you have to write it once where it executes, and once where it prints. Using eval allows you to turn that idea inside out (sort of), and divide your program into two pieces, one of which prints the other half, and itself, but doesn't execute... but the other piece can execute the first piece. That way, the majority of your code only needs to be written once.

my $program = <<'END'; # put any arbitrary code here print "my \$program = <<'END';\n${program}END\neval \$program\n"; END eval $program

(Oh, and by the way... would it help you to understand your code if you knew that 34 was the ascii character code for the quotation mark, and that %c is the printf marker for printing a character by its ascii character code? Good luck... the recursion theorem is fun stuff.)

Update: one too many newlines caused the quine to grow by an empty line with each run. sloppy.

------------ :Wq Not an editor command: Wq

In reply to Re: Self-printing program by etcshadow
in thread Self-printing program by jpfarmer

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.