obfuscating Perl for fun and profit
(apologies for "promoting"(?) Perl obfuscation...)
Today I won a gift card at an in-office meeting with the following code. Challenge: print the numbers 1-100 in the most incomprehensible, inefficient way. My entry, edited for brevity:
#!/usr/bin/env perl
use v5.16;
splice @_, @_, -1, ++$_;
splice @_, @_, -1, ++$_;
splice @_, @_, -1, ++$_;
splice @_, @_, -1, ++$_;
splice @_, @_, -1, ++$_;
# plus 95 more of this
say join $/, @_;
Thinking about it more this evening, I came up with
$SIG {__DIE__} = sub { $_ = (pop)+0; chomp; $_%6?say:exit};
{ select undef,undef,undef,1; eval { die time-$^T }; redo; }
(where 6 instead of 101 so I don't have to wait 100 seconds (and to be honest I'm not sure if there'll be rounding errors)).
Wonder if any obfuscators could come up with better (the less inefficient, incomprehensible the better).
%6 is too straight forward. Might go with something like '$^T%int($^F*($^F**$^F)+$^F)*int($^F*($^F**$^F)+$^F)' (assuming your max system file descriptor is two) for $^T%100 or some other bit of line noise if you have your heart set on stopping at six.
Bleh, needed one more set of ()s around the line noise that means 100, so '$^T%(int($^F*($^F**$^F)+$^F)*int($^F*($^F**$^F)+$^F))'.
Nice. I got it to work (print 1 - 100) with:
$_%(int($^F*($^F**$^F)+$^F)*int($^F*($^F**$^F)+$^F)+$^F**0)?say:exit
(note: $_ is time-$^T rather than $^T, and +$^F**0 adds 1 to make 101, since it exits after printing 100)
I kinda dislike the overall part too, but haven't figured that out yet.I guess you could nest some extra ternary statements there to make it more confusing but I can't think of any good ones at the moment. Something that always evaluates to "exit" but does it an extremely impractical way. Maybe building the ascii codes for the word "exit" with some pointless math, chr them, put them into an array, run a join on the array, and then eval the end result scalar.
It's still a solid operation for obfuscated perl code though, especially without the traditional spacing, on account of about a quarter of the people looking at it will have to take a moment to understand it.