Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-03-29 09:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found