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?.


In reply to Short quines by ambrus

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.