http://qs321.pair.com?node_id=661934

This node contains a couple of short non-empty non-cheating perl quines. I've had most of this collection for ages, but have apparently never posted them on perlmonks.

More precisely, a perl quine is a perl script that when run with perl without any command line arguments, it outputs precisely its source code on stdout. (If you also allow stderr instead of stdout, you can get a little shorter by changing print to warn in some of these. I'm not sure which definition is better.) Some quines are considered cheating, namely those that read their own source code from the beginning, e.g. with the famous open 0 trick. It is also considered cheating to depend on the filename of the source code, or an environment variable you have to set beforehand, or, in general, any information source that's outside the source code which you have to specifically set up; that is, the quines should just work without any special tricks on how to invoke them.

Before each quine, I list a short name for identifying it, the length of its source code in bytes, and the number of non-whitespace bytes in its source.

Note that 323b and 323e end in two newlines; 323a and 323g end in one newline; while 323c, 323d, 323f and 323h ends in a graphic character.

One of these quines (323e) is the shortest non-empty one I know, but it has the disadvantage that it only works in perl 5.6.0 or later.

323a: 44 40

print<DATA>x2 __END__ print<DATA>x2 __END__

323b: 35 32

print<<''x2,"\n" print<<''x2,"\n"

323c: 44 44

$a=q(;print"\$a=q($a)$a");print"\$a=q($a)$a"

323d: 50 48

$\="\47";print,print for'$\="\47";print,print for'

323e: 31 26

print<< x2,v10 print<< x2,v10

323f: 46 44

$\=v39;print,print for'$\=v39;print,print for'

323g: 58 57

print"$_\Q$_\E\"\n"for"print\"\$_\\Q\$_\\E\\\"\\n\"for\""

323h: 52 52

print"$_\Q$_\E\""for"print\"\$_\\Q\$_\\E\\\"\"for\""


Update 2012-12-12: see also General quine, How I Learned About Quines, Quine Whine, Is there a non-empty error quine in perl?.