| « Adding complexity to reduce complexity | Perls of Visdom » |
Thu, Mar 15, 2012
![[Icon]](rsc/img/chain_link.gif)
There's been yet another complaint doing the rounds about things you put onto Facebook/Twitter/Google+ not being kept private.
I personally think anyone who puts anything onto any such site that they don't consider "public domain" is simply insane, so I can't say I was interested enough in whatever the latest outcry is about to actually read it.
But it did remind me of a joke I saw once where somebody said that, to keep their messges private, they encrypted them with ROT13 at least twice, sometimes four or even six times!
ROT13, for those who don't know, moves the letters of the alphabet 13 characters along - so A<->N, B<->O, C<->P etc. So doing it twice takes you full-circle and leaves a message unaltered.
But that, in turn, reminded me of an old UserFriendly strip which was itself ROT13'd. And I'm trying to get more practice at writing perl one-liners, so I had a quick go at writing a ROT13 , as it was lunchtime.
It turned out to be laughably simple. The script would be:
while (my $input = <STDIN>){
$input =~ tr#[a-m][n-z]#[n-z][a-m]#;
print $input;
}
But there's no reason not to just use $_ instead of assigning a variable when turning it into a one-liner.
perl -e 'while (<STDIN>){ $_ =~ tr#[a-m][n-z]#[n-z][a-m]#; print $_; }'
And then, of course, since $_ is used by default, it can be dropped altogether:
perl -e 'while (<STDIN>){ tr#[a-m][n-z]#[n-z][a-m]#; print; }'
And that's readable enough - whilst you have input from STDIN, translate the letters appropriately & print them.
But referring to famous perl one-liners, I was able to refine it a bit further.
Firstly, <STDIN> is implied by just <> - I didn't realise that until now. And then calling perl with -p implies a while(<>) loop AND prints the output for you. So you can actually shrink the whole thing down to just:
perl -pe 'tr#[a-m][n-z]#[n-z][a-m]#;'
Not bad for a complete ROT13 program, I think.
Feel free to use this to protect your own tweets & status updates as many times as you like ;)