Examples of quines in Perl 5
This first example, while a valid quine, is considered "cheating" by those in the know , since it opens itself and simply scans its own source code:
#! /usr/bin/perl -w
# This is not really a quine:
open (Q, $0);
while (<Q>) {
print;
}
This next example is considered a quine amongst the Perl community, but it still seems like cheating to me, using a syntactic trick of Perl to do it's job. It takes advantage of the fact that Perl will scan the __DATA__ structure before it is even declared:
#! /usr/bin/perl
undef $/;
$_ = <DATA>;
print;
print;
__DATA__
#! /usr/bin/perl
undef $/;
$_ = <DATA>;
print;
print;
__DATA__
Here is another example that is a little more fun, courtesy of Blake Mills (Blakem on perlmonks):
#!/usr/bin/perl
printme(q{
sub printme {
print "#!/usr/bin/perl\n\n";
print "printme(q{$_[0]});\n";
print "$_[0]";
}
});
sub printme {
print "#!/usr/bin/perl\n\n";
print "printme(q{$_[0]});\n";
print "$_[0]";
}
Now is where it starts getting really interesting. Try and GOLF your own in any language!