Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Comment on

( #3333=superdoc: 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'

In reply to step-by-step JAPH by cog

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others drinking their drinks and smoking their pipes about the Monastery: (13)
    As of 2014-10-20 09:53 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      For retirement, I am banking on:










      Results (75 votes), past polls