In my programmatic quest to shrink all lines of code into one line, I have managed to tune my Perl scripts recently at work into 1-3 line wonders. I've been influenced a bit (perhaps in a geeky negative way) by some people over at ValueClick in the past in creating obfuscated, but killer Perl code. The great thing is that I'm learning how to really utilize some of the more lesser known features that you probably won't see much in anything but the most hardcore type of modules out there.
For instance, I started using the following paradigm:
sub func {
+{ map { $_->{something} => $_->{value} } @{$_[0]->someMethod($_[1], $_[2])} };
}
this is a cool little piece of code that i finally got to use the +{. I found that little bit of code in the O'Reilly SOAP book, the author of SOAP::Lite.pm had a few statements like that. I scratched my head for a while trying to figure out what it did. Later, I found out that it's a type hint to the interpreter to return back a hash reference.
I used it here because if I just did something like:
{ map { ....} };
I'd receive an array, which isn't what I wanted. The +{ sign will convert this into the appropriate hash reference. I showed my boss to see if he knew what the heck it did, but it's a pretty obscure thing in Perl that I don't see often times used. But I'm finding it immensely useful with statements like that.
Of course, your next comment might be, well why the heck would you need a statement like that in the first place? I'm working quite a bit with various types of data. Sometimes, I have lists of data in the array format. But I occasionally want to convert it over to a hash format. Map partly helps here, but if I want to stay true to my virtue of one liners and avoid returning another array, then the +{ becomes quite useful in conjunction with map.
I really enjoy programming in Perl the most because it's like a puzzle where you're constantly trying to optimize what you're doing. I've seen people use tons of variables or less obscure syntax, but I think that it's really not that great of a thing in the end. I mean, in theory, you're replicating memory by creating more variables and the function call potentially grows, whereas a one liner, for a true veteran of Perl, can easily be deciphered without having to painfully scroll through unnecessary pieces of code. I mean, if you want verbose, code Java or COBOL. Then tell me how readable your code is and how long it took you to get a simple idea out the door.
Trackbacks: (Trackback URL)
No Comments Posted Yet
