Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explanation for perl quine

Tags:

perl

quine

I found this quine recently

$a='$a=%c%s%c;printf($a,39,$a,39,10);%c';printf($a,39,$a,39,10);

And I just can't get my head around it. I've found no explanation on google/SO for this particular one, so I'm hoping that someone could explain to me how that thing works :-)

like image 772
Vince Avatar asked Sep 18 '25 20:09

Vince


1 Answers

This code is consisted of two lines.

A variable assignment:

$a='$a=%c%s%c;printf($a,39,$a,39,10);%c';

And a printf:

printf($a,39,$a,39,10)

First of all let's learn a few things about printf. If you check the sprintf documentation you will see that printf('%c',39) will print the 39th character which is ', while the printf('%c',10) is a newline \n. Another thing to keep in mind is that printf takes a list of parameters which means that printf('%s%s','foo','bar') will print foobar.

So now it should be clear that printf($a,39,$a,39,10) takes the format from $a and does the following 4 conversions (equal to the number of % signs in the $a string)

  • uses the 2nd argument 39 for the 1st occurrence of %c (check the $a variable),
  • then it uses the string $a for %s
  • the 4th argument 39 of the printf for the next %c
  • and finally the 10 for the last %c

which results in a copy of its own source code.

like image 134
psxls Avatar answered Sep 20 '25 15:09

psxls