in reply to Self-printing program

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

Replies are listed 'Best First'.
Re: Re: Self-printing program
by jpfarmer (Pilgrim) on Apr 02, 2004 at 18:11 UTC

    You're example using eval is excellent. I attempted something similar earlier, but my recursive call wasn't correct, but looking at your example, I understand why.

    The other part of this program is to make the program output and arbitrary range of program lines, and trying to duplicate that code via the method I was using earlier was a real pain. Using this method, it should be much easier, although I wish I could execute code that was in an array directly, because it would make my life easier.

    UPDATE: I implemented my existing code using this method, and it works very, very well. Thank you again for your help!
      Well, while this method is neat, note that it is not exactly the same thing as the recursion theorem. You should really understand the recurion theorem, or you'll probably be sorry, in the end.

      The simple way to add arbitrary code to a (recursion-theorem-based) quine is like so:

      $body = ' $body = %c%s%c; foo(); bar("baz"); printf($body,39,$body,39); '; foo(); bar("baz"); printf($body,39,$body,39);
      Note that, when doing it this way (as I mentioned), you end up having to duplicate the code (once to print it, and once to do it).
      ------------ :Wq Not an editor command: Wq