Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

step-by-step JAPH

by cog (Parson)
on Sep 21, 2004 at 09:51 UTC ( [id://392592]=obfuscated: print w/replies, xml ) Need Help??

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'

Replies are listed 'Best First'.
Re: step-by-step JAPH
by cchampion (Curate) on Sep 21, 2004 at 10:36 UTC
Re: step-by-step JAPH
by bart (Canon) on Sep 28, 2004 at 12:21 UTC
    Now we use decimal code for the "print" word
    \160\162\151\156\164
    That's not in decimal, that's octal.
      Oops... you're absolutely right...

      There, fixed. Thanks.

•Re: step-by-step JAPH
by merlyn (Sage) on Sep 28, 2004 at 10:49 UTC
    Please keep in mind that the canonical JAPH, as demonstrated by me for many years, generates precisely the 25-character string of: "Just another Perl hacker," including the comma and that precise capitalization.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      Including a newline?

      What about LF vs CR/LF vs CR?

      Your specification is vague! :-)

      Hmm, 25-character string. Oh well, there goes my joke. :-)

      Makeshifts last the longest.

      I'll try to fix the slides for my talk next time I give it O:-)
Re: step-by-step JAPH
by theroninwins (Friar) on Sep 22, 2004 at 07:04 UTC
    Yes I must admit it was very entertaining. I really enjoyed the read.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: obfuscated [id://392592]
Approved by htoug
Front-paged by rnahi
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (7)
As of 2024-03-29 13:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found