Is there anywhere that Joe Humanities Degree can go to to learn how to do this sort of thing?
Needless to say, this sort of request warms the cockles of my heart (whatever those are). And today, in the aftermath of various recent demonstrations of scripting languages’ utility, it might be a good time for me to press my case.
So: yes! Yes, a thousand times yes. You can learn a scripting language, which is the easy way to put together an IM answerbot, or a ballot-stuffing bot, or whatever else. In fact, you not only can but probably should. Scripting languages are a powerful way to control computerized systems. The computerization of the world is obviously nowhere near complete, but it’s getting there quickly — and that means that the relevance and power of scripting languages are only going to increase. You can already control various pieces of the physical world with this technology, like lights and thermostats. And if you use Excel or perform repetitive tasks on a computer in your daily life, you can likely save yourself some drudgery with a well-written script. Soon enough your toaster, garage door and television will be accessible through these languages, too.
There will be other ways to control the electronic world, it’s true. People will continue to invent visual metaphors like Yahoo! Pipes and Lego Mindstorms that expose some of the capabilities of programming in an accessible way. But we’re a long way from the day when these GUIs confer the same power that knowledge of a scripting language affords. Besides, they tend to rely on metaphors that are more easily learned through a text-based programming language.
And that’s important. Along with the ability to manipulate the devices and information around you, learning a little programming will help you understand how engineers think. If you familiarize yourself with the sausage-making side of computers you’ll begin to develop an intuitive understanding of hardware and software interfaces which, as I mentioned, will comprise a larger and larger portion of the world. You’ll never have to be one of those hopeless codgers reduced to asking his or her grandkids for help programming the hyper-VCR.
So how should you approach this worthy undertaking? The best way is probably to pick a scripting language and start perusing the materials that are available. But which scripting language? In some ways it doesn’t matter — you’ll need to learn what a variable is, how an if..then statement works and what a for loop does in any of them. But you may as well hear the nerd scuttlebutt on each and then make an informed choice. So let’s look at the big three first:
- Perl’s one of the oldest scripting languages still in widespread use, and powered much of the early web. Its biggest advantage is probably CPAN, a massive library of add-ons written by people to handle the hard part of whatever neat thing you want to do, whether it’s the creation of an IM bot, manipulating Flickr or the automatic generation of charts. It’s also very nice for processing text, which, after all, is what the net is mostly comprised of. And because of its age you can find it preinstalled and available on most webhosts and Unix or Mac systems (it’s easy to get running on Windows, too).
The knock on Perl is that it’s slow and somewhat byzantine. Most importantly, it’s relatively hard to read Perl code (much harder than it is to write it), which makes learning by example difficult. As you may have noticed I use Perl a lot for tasks where speed isn’t important, both because I’m used to it and because CPAN makes so many excellent tools available.
If you want to learn Perl, I’d recommend that you start by reading the freely available PDF of the book Beginning Perl.
- It’s the cool new kid on the block. Ruby’s very chic, and is frequently lauded for producing “beautiful” code. It really is a pretty elegant language. It’s also the underpinnings of the Ruby On Rails framework, which is a new and very popular way to get a dynamic, database-driven website up and running with minimal effort. Ruby’s cool, and is the next language on my list of things to learn.
But it’s still a work in progress. It’s a little slow, although that’s likely to improve. And its RubyGems package manager system — Ruby’s version of CPAN — is easy to use, but not yet all that fleshed out. If you want to talk to Flickr, for instance, you’ll find that most of the available Gems have serious limitations. It’ll get better, though, and these sorts of problems aren’t that relevant for beginners.
There are also particularly great learning tools for Ruby, many of them thanks to one man called Why. You should definitely have a look at Try Ruby, an in-browser demonstration of the language that walks you through a basic tutorial. After that, LearnRuby.com is probably the best clearinghouse for traditional learning materials. But if you like your programming primers weird, illustrated and told through elaborate, nonsensical stories, you should absolutely read Why’s (Poignant) Guide To Ruby. It’s definitely the strangest programming book I’ve ever read.
- Python kind of splits the difference. It’s not as old as Perl and not as new as Ruby. It’s got a lot of tools available for it, but perhaps not as many as Perl (it’s better-designed, though). It’s got its own hot web development framework — one that’s not quite as exciting as Rails but seems to have fewer scaling problems. It can be made blazingly fast when necessary — Google uses Python extensively, and it sort of seems like they know what they’re doing.
I don’t really care for it, though. I tried to learn Python by writing a series of bloggy tutorials way back when. But not only was I not very good at keeping my instruction properly focused, I also didn’t end up liking the language very much. The weird markup bugged me (it controls program flow with whitespace in an unusual way), but the sin for which I’ll never forgive Python is its awkward syntax for using regular expressions. Regexes are a common feature across languages, and are an incredibly powerful way to process text. Python requires a preparatory step before using them; Perl and Ruby don’t. For that reason I’m unlikely to dive back into Python.
Wolfson loves it, though. You may want to talk to him if you’re interested in the language — I’m sure he can make a better case for it, and point you toward good resources for learning it.
As I said, those are the big three. There are countless others, but these are the ones that you’ll run into the most. They’re used all the time to process data and glue applications together. Any one of them would be a fine choice.
But there are a couple of less conventional options that I think you may want to consider, too:
- If you have a Mac, you have AppleScript. I don’t know it myself, but it’s an extremely readable language designed to let you automate tasks on your Mac. The applications it lets you control will be immediately familiar — you’ll be able to fetch a webpage with Safari rather than, say, wget or curl. Consequently you may be able to perform impressive feats more quickly than you would with a more general-purpose language. This PDF seems like a good place to start.
- PHP is widely-used, but generally in a web context (“Hypertext Processor” makes up the final two thirds of the acronym). If you buy a cheap webhosting account, it probably comes with PHP. This entry exists on a page served by PHP. Recent versions of OS X have it installed by default, too.
It’s not frequently used for scripts that run for a long time — web pages need to be served quickly, after all. But you can run it from the command line just fine, if you’d like (our previous sysadmin had most of his maintenance scripts written in PHP). You may have to adjust its automatic timeout period upward if you use it this way, though (scripts that take too long to run are automatically terminated by default, since they usually represent a misbehaving web page that’s a threat to system performance or stability).
PHP is widely available, fast, easy to read, uses a syntax that’s a sort of mutt conglomeration of other languages (rather than an idiosyncratic dead-end), and has a ton of stuff built in — if you want to generate graphics or process PDFs, the functionality is likely already available rather than existing in an add-on you need to install.
PHP has a bad reputation because it’s so easy to use — lots of lousy code has been written in it. It’s also not object-oriented (although it pretends to be), which is important to professional programmers but mostly irrelevant to beginners. But there’s tons of good documentation: nearly every page on PHP.net contains dozens of comments from users offering helpful tips and sample code.
If you think your scripting efforts might primarily end up living on the web, PHP is probably the best choice for you. Here’s a good starting point.
That’s it. At the very least you should poke through some starter documentation and get a sense of what’s involved. You can get a lot done with a relatively small amount of programming knowledge. And of course if you’ve got a particular question you should feel free to leave a comment or send me an email. As you can probably tell, I’m anxious to share the scripting gospel.