Hack 101. Learn from Obfuscations


Learn more about Perl from the play of others.

Perl has a reputation for serious play. Think of Perl golf (solving problems in the fewest characters possible), JAPHS (printing a simple message in creative ways), and obfuscation (writing odd code that does surprising things). Though you'd never use these tricks in production code, producing such creative programs requires careful study and explorationboth tricks of good hackers.

Exploring obfuscation can also expand your Perl skills.

Consider an obfuscation I posted at Perl Monks (http://www.perlmonks.org/index.pl?node_id=77619; the link includes a deconstruction and explanation by Guildenstern). It is a non-traditional JAPH that is self-referential. Sort of. The use of a variable called pi, the use of the sin function, and the visual layout of the code all hint at what the output will be. The irony of course is that while the layout helps you know what to expect, it actually hinders understanding.

#!/usr/bin/perl                              # how to (ab)use substr use warnings; use strict; my $pi='3.14159210535152623346475240375062163750446240333543375062';      substr      ($^X,0)=        substr    ($pi,-6);map{          substr  ($^X,$.++,1)=chr(           substr ($pi,21,2)+           substr ($pi,$_,2))}(12,28,-18,-6,-10,14);map{$^O=$"x(          substr  ($pi,-5,2));        substr    ($^O,sin(++$a/8)*32+      substr      ($pi,-2)/2+1,1)=$_;    substr        ($^O,sin($a/4)*(  substr          ($pi,2,2))+ substr           ($pi,-7,-5)-1,1)=$_;print"$^O$/";eval($^X.('$b,'x3). substr           ($pi,-3,1).'.'.  substr          ($pi,9,2));}(map{chr($_+    substr        ($pi,21,2))}(      substr      ($pi,8)x6)=~/../g);

"So", you may think, "what could be the pedagogical value of this rather ridiculous piece of code?". I believe its value lies in its ability to raise questions that cause the curious to seek out answers.

Complete beginners might inquire:

  • l00k5 k3w1 d00d!!! Wh4t l4ngu4g3 15 th4t?

  • Is this really a computer program that runs and does something?

Those with a little exposure to Perl may ask:

  • Does Perl allow such bizarre formatting without throwing errors?

  • Can you really create a number with that many digits of accuracy?

  • What does substr do? Is it like the C function?

More experienced programmers may wonder:

  • How does the animation work with only one print statement and no for or while loops?

  • You can put substr on the left hand side?

  • Are there both two and three argument forms of substr?

  • I've figured out that there's a select call in the program, but what's it doing?

  • How come use strict; and use warnings; don't complain? Aren't they supposed to ensure the quality of your code?

  • Why didn't he have to declare $a and $b using my?

As for myself, the value of this obfu was in the enjoyment I received from creating and sharing it with the Perl community, and in the comments and feedback I received because of it. I consider the few hours that it took to write it time well spent.

When you come across Perl play in your travels, certainly do take apart the code with B::Deparse and pore through perldoc perlfunc and perldoc perlvar to find out what's going on. Then take a step back to ask the deeper questions and ponder how you too can assemble your own creations from Perl's rich vocabulary.

Happy coding!



Perl Hacks
Perl Hacks: Tips & Tools for Programming, Debugging, and Surviving
ISBN: 0596526741
EAN: 2147483647
Year: 2004
Pages: 141

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net