Learn to scratch your own itch

I look at the world around me and feel almost disgusted by the entrenched defaultism that I see everywhere. For instance, when the internet came along there was a sense of liberation from the passivity of watching television. We learnt to talk back. We learnt to create our own blogs and express ourselves as opposed to merely imbibing the thoughts of others in a mass daily dose of benign hypnosis. Clay Shirky informed us about the great cognitive surplus that would result from being so freed. And yet here we all are – facebook members all – allowing one site to define the structure of our social relationships. Yes we can comment. Yes we can poke. It’s more than television allowed. But it’s the whole world still watching one tube, one interface – just as it was before. As always we accept the tools on offer without ever questioning whether or not our desires and needs extend beyond it.

Many of you can’t imagine this because you’ve never had the experience of having your desires open out in the sort of way I mean. The way the interfaces with which you interact constrain your awareness of those desires, because as far as you are concened – they exist outside the realm of [imagination]. ~ Dan Haggard

Seth's Blog: The myth of preparation

I’m all for expertise. Experts, people who push through and make something stunning—we need more of them. But let’s be honest, if you’re not in the habit of being an expert, it’s unlikely your current mode of operation is going to change that any time soon.

Go, give a speech. Go, start a blog. Go, ship that thing that you’ve been hiding. Begin, begin, begin and then improve. Being a novice is way overrated.Seth Godin

Adding style to perl code

Perl has always been my favourite command line and web programming language. There is nothing Perl can't do. The Perl language combined with the large quantities of modules on CPAN (developed by some of the best programmers on the planet) makes it the kitchen sink of programming languages.

I like to make my code as easy to read as possible. Nothing irks me more than a jumble of spaghetti code with indents and curly braces everywhere. I admit that there are times when a solution to a complex problem comes to me, and I become more concerned with getting my ideas down than with following style rules. But after I have my thoughts properly coded, I want to go back and clean things up a bit. That's where perltidy comes in.

What is Perl::Tidy?

Perltidy is a Perl script which indents and reformats Perl scripts to make them easier to read.

The CPAN module, written by Steve Hancock, makes the functionality of the perltidy utility available to other Perl scripts. That means you can write your script for your own Perl-style needs. I prefer to use the utility.

Installation

Installing Perl::Tidy on OS X is relatively easy. Use the sudo command from the Terminal to run the CPAN command line utility. You need to be logged into an account with administrator privileges. This will pull down and install from CPAN, all the necessary code for using perltidy.

Using perltidy

Once the module has been installed (the utility is placed in /usr/bin ), you can use the command line utility just like any command in Terminal. Perltidy has several command line switches to modify its behaviour. The default styling works well enough for me. To use the command line utility, open up a Terminal window and type :~ perltidy -b /path/to/your/code.pl

I used the -b switch to tell perltidy to create a backup of my script before doing its work. I want a copy just in case something goes wrong. Perltidy does its thing and then exits. If errors are found, e.g. a missing curly brace, perltidy will let you know and create an error file that contains more information about the problem.

Perltidy will take this code:

my %var = { name => ‘khurt’, last => ‘williams’, location => ‘Princeton’, height => ‘164cm’, weight => ‘64 kg’ }; foreach my $key ( sort keys %var ) { printif( “%s:%sn”, $key, $var{$key} ); } #this prints the values in the hash

And turn it into this code:

my %var = {
    name     => 'khurt',
    last     => 'williams',
    location => 'Princeton',
    height   => '164cm',
    weight   => '64 kg'
};
foreach my $key ( sort keys %var ) {
    printif( "%s:%sn", $key, $var{$key} );
}    #this prints the values in the hash

Useful perltidy command:

: ~ perltidy -html -pre code.pl

This will create an HTML snippet with only the PRE section to code.pl.html. This is useful when code snippets are formatted for inclusion in a larger web page (such as a blog).

To learn more about perltidy, type "man perltidy" into Terminal to see the manual page. Prepare to be overwhelmed. Perltidy is feature-rich.