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

I gave a talk entitled "Perl Black Magic" on YAPC::EU::2004, in Belfast. The talk was on obfuscation and golfing.

One of the examples I used was a step-by-step JAPH. It follows:

We start off with something simple


print "Just another Perl hacker"

Let's split the words


print "Just ", "another ", "Perl ", "hacker"

Instead of printing spaces, let's use the $, variable (it's contents are printed between elements of an array being printed)


$, = " ";
print "Just", "another", "Perl", "hacker"

Let's reverse the word list and apply the reverse function to it


$, = " ";
print reverse "hacker", "Perl", "another", "Just"

Instead of printing the words, let's add them to an array and print the array in the end


$, = " ";
push@_,$_ for reverse "hacker", "Perl", "another", "Just";
print @_

Instead of assigning a space to $, , let's assign the $" variable directly (it contains a space, by default)


$, = $" ;
push@_,$_ for reverse "hacker", "Perl", "another", "Just";
print @_

Using a plus sign with the $" variable produces no undesired side effect


$, =+$" ;
push@_,$_ for reverse "hacker", "Perl", "another", "Just";
print @_

Likewise, with $,


+$,=+$";
push@_,$_ for reverse "hacker", "Perl", "another", "Just";
print @_

Instead of printing the array @_, let's join it's elements inside the $_ variable; we do it with a substitution


+$,=+$";
push@_,$_ for reverse "hacker", "Perl", "another", "Just"; 
s//join$,,@_/e;
print $_

Now we put that instruction inside a string, and eval two times instead of one


+$,=+$";
push@_,$_ for reverse "hacker", "Perl", "another", "Just"; 
s//"join".'$,,@_'/ee;
print $_

We use hexadecimal instead of ascii, in the "join" instruction


+$,=+$";
push@_,$_ for reverse "hacker", "Perl", "another", "Just"; 
s//"\x6a\x6f\x69\x6e".'$,,@_'/ee;
print $_

We use the array @, instead of @_ (thus creating more entropy)


+$,=+$";
push@,,$_ for reverse "hacker", "Perl", "another", "Just"; 
s//"\x6a\x6f\x69\x6e".'$,,@,'/ee;
print $_

We ommit the $_ in the "print" instruction


+$,=+$";
push@,,$_ for reverse "hacker", "Perl", "another", "Just"; 
s//"\x6a\x6f\x69\x6e".'$,,@,'/ee;
print 

Instead of backslashes, our substitution now uses braces and two semicolons


+$,=+$";
push@,,$_ for reverse "hacker", "Perl", "another", "Just"; 
s{};"\x6a\x6f\x69\x6e".'$,,@,';ee;
print 

We now use a dot to concatenate the result of the two last expressions. The concatenation really isn't important, because we're not doing anything with its result. The important thing is that instead of doing two operations (substitution and print) one at a time, separated by a semicolon, we now use a dot to concanate their result. Both instructions are evaluated, but in a more confusing way to human eyes


+$,=+$";
push@,,$_ for reverse "hacker", "Perl", "another", "Just"; 
s{};"\x6a\x6f\x69\x6e".'$,,@,';ee.
print 

Our string to be printed now includes the "print" instruction and we now evaluate that string instead of printing it


+$,=+$";
push@,,$_ for reverse "hacker", "Perl", "another", "Just"; 
s{};"print \x6a\x6f\x69\x6e".'$,,@,';ee.
eval

We now eval the string directly in the substitution (notice the three 'e's we now have)


+$,=+$";
push@,,$_ for reverse "hacker", "Perl", "another", "Just"; 
s{};"print \x6a\x6f\x69\x6e".'$,,@,';eee

Now we use octal code for the "print" word


+$,=+$";
push@,,$_ for reverse "hacker", "Perl", "another", "Just"; 
s{};"\160\162\151\156\164 \x6a\x6f\x69\x6e".'$,,@,';eee

We remove some unnecessary spaces


+$,=+$";
push@,,$_ for reverse"hacker","Perl","another","Just";      
s{};"\160\162\151\156\164 \x6a\x6f\x69\x6e".'$,,@,';eee

We put brackets around the first instruction and concatenate its result with the for cycle


(+$,=+$").
push@,,$_ for reverse"hacker","Perl","another","Just";     
s{};"\160\162\151\156\164 \x6a\x6f\x69\x6e".'$,,@,';eee

Let's put everything in two lines


#!/usr/bin/perl
(+$,=+$").push@,,$_ for reverse"hacker","Perl","another","J".
"ust";s{};"\160\162\151\156\164 \x6a\x6f\x69\x6e".'$,,@,';eee

And we're done :-) A JAPH, step-by-step :-)

I hope you liked it :-)

-- perl -e 's..g.g.s..o.o.s..c.c..print'