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:
Those with a little exposure to Perl may ask:
More experienced programmers may wonder:
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! |