Perl obfuscations

The flexibility of Perl allows for extremely nicely looking code. On the other hand, the same flexibility allows people to do ugly things, which Perl is very well known for. Sometimes, though, I come across a piece of code which is both ugly and beautiful. Here is one example written by one of my collegues:

sub _uintvar {
    my ($v) = @_;
    $v = sprintf("%b",$v);
    $v = '0'x(-length($v)%7).$v;
    my @v = map {"1$_"} $v=~/(.{7})/g;
    substr($v[-1],0,1) = "0";
    $v = pack("B*",join('',@v));

This was found in the production system. It works. And it was tested.

Now imagine that the name of the subroutine would be different in some non-selfdescriptive way. Would you be able to parse it, understand it, and say what it does? How about fixing a bug in there?

One thought on “Perl obfuscations”

Leave a Comment