Matthew Daly's Blog

I'm a web developer in Norfolk. This is my blog...

19th October 2010 2:52 pm

I Passed!

Just a short post to say that today I did my CIW Perl Fundamentals exam, and passed it. Glad that’s out of the way!

16th October 2010 9:11 pm

Another Perl Snippet...

Here’s another simple Perl program I thought I’d share. It’s a very simple app for recording notes from the shell. As pasting the last one into WordPress didn’t work too well, it’s on my Dropbox account instead. I’ve used SQLite as the backend for the database (a no-brainer - I really like SQLite for little quick-and-dirty scripts like this) and it works pretty well.

At some point I may go back to this basic concept and expand on it a little - it would make sense to add a GUI to it, either by using a graphical toolkit such as Tk or turning it into a web app. I also like the idea of adapting it to use a non-relational database - something like CouchDB perhaps.

15th October 2010 8:39 pm

Getting Work Done With Perl

After my initial struggles with Perl, I now think I’m really starting to get to grips with the language. I generally find it a pain when you have to learn by building small but basically useless scripts - I always do best when building something useful.

As one of the exercises for my studies I had to open a database connection to a Microsoft Access database, but I wanted to do the exercise in Ubuntu (I’ve always preferred using Unix-like operating systems for programming, and thanks to apt-get it’s a lot less grief installing additional libraries and modules as you need them) and couldn’t get Perl to connect to the database properly, so I resolved to export it to either MySQL or SQLite.

I was able to export it to MySQL in the end using mdbtools, but I wasn’t entirely happy with the end result. I resorted to re-exporting the data as a CSV file, then resolved to write a small Perl script to read the file, parse it using a regular expression to obtain the necessary information, then insert it into a new SQLite database.

Here’s what I came up with:

#!/usr/bin/perl -w
use strict;
use DBI;
my $db = "dbi:SQLite:backend.db";
if (!(-e "backend.db"))
{
print "Database does not exist. Creating it...";
# Create the database
my $dbh = DBI->connect($db) or die "Error in connecting to database! $DBI::errstr";
my $createdb = $dbh->do("CREATE TABLE CARS( ID INTEGER PRIMARY KEY, YEAR INTEGER, MAKE VARCHAR(30), MODEL VARCHAR(30), COLOR VARCHAR(30), PRICE INTEGER);");
$dbh->disconnect();
}
open(READFILE, "cartable.csv");
while()
{
unless($_ =~ m/id,/)
{
m/\d+\,(\d{4})\,\"(\w+)\"\,\"(\w+)\"\,\"(\w+)\"\,\"(\d+)\"/;
my $year = $1;
my $make = $2;
my $model = $3;
my $color = $4;
my $price = $5;
my $dbh2 = DBI->connect($db) or die "Error in connecting to database! $DBI::errstr";
my $insertdb = $dbh2->do("INSERT INTO CARS (YEAR, MAKE, MODEL, COLOR, PRICE) VALUES (\"$year\", \"$make\", \"$model\", \"$color\", \"$price\");");
$dbh2->disconnect;
}
}
close(READFILE);
print "Write completed!\n";
print "To demonstrate it works, we'll run a SELECT query against the database...\n";
# Read the database
my $readdb = DBI->connect($db);
my $dbselect = $readdb->prepare("SELECT * FROM CARS;");
$dbselect->execute;
# Print the results
print "ID\tYear\tMake\tModel\tColor\tPrice\n";
while(my @row = $dbselect->fetchrow_array)
{
print "$row[0]\t$row[1]\t$row[2]\t$row[3]\t$row[4]\t$row[5]\n";
}
# Close the connection
$readdb->disconnect;

Apologies for the fact that the indentation doesn’t seem to have copied across from Vim very well (can anyone recommend a good WordPress plugin for displaying code, none of the ones I’ve tried seem to be any good?). It works well, and it’s also helped me grasp Perl’s database API better.

I think I’ve got a better idea now of what Python and Perl are best at and when to use each. Perl is a great language, but the fact that a lot of it is implicit makes it a little harder to pick up at first than Python - for instance, the default variable, which is quite a good idea, but takes a little getting used to. Its regex support is great, and I like the database API, but I would find it a lot harder to do any object-oriented programming in Perl than in Python (which I guess is why Moose exists). I’ve found Perl very useful for quick and dirty scripts and as a glue language, but for longer scripts Python seems the better choice.

22nd August 2010 5:15 pm

What Makes a Good Programming Textbook?

I own a lot of programming textbooks. I went through a long phase of buying ones about virtually any technology I was even remotely interested in, therefore I own loads of books about Perl, Ruby, Python, PHP and C, among others. Granted, with many of them I’ve done little more than flick through them (I find it’s hard to get round to learning things like that without some kind of plan, which was what made me eventually start doing a more formal course since it forced a plan on me), but I’ve seen quite a few.

But of course, not every textbook is equal. Some are great, truly seminal works that are raved about by well-known programmers. Examples include the Camel book (Programming Perl) and K & R (The C Programming Language). Others are rarely mentioned. But what makes a really good textbook? Here I’m going to list some of the attributes that I’ve found in my favourite and most effective programming texts, and that I think make for a good, effective and informative textbook that makes a good job of getting you up and running programming in a new language:

  • Lots of working examples to enter - To learn to program, whether from scratch or in a brand new language, the best advice I’ve ever heard was that you need to read a lot of code, and write a lot of code. I find that, at least at the start, nothing helps me learn to code in a new language better than lots of examples for me to type in and run, in order to pick up the basic syntax and keywords of the language. After all, that’s how many people used to learn BASIC, by typing in listings from magazines, and it’s how you learn English as a child - you’re exposed to the language, and you copy it, then understanding comes later. One of the best examples of this is C for Dummies, All In One Desktop Reference, by Dan Godkin - it’s packed full of loads of great example programs to enter and run that demonstrate the basic concepts well in C.
  • Maintains your interest by showing you how to do interesting things - Not many people are interested in learning a new programming language to do something tedious (that said, if someone already has to do something tedious, such as a task at work, teaching them how to write a program to do it for them may well be considerably more interesting for them than doing the task themselves, hence the popularity of scripting languages for automating dull tasks), so a good programming textbook needs to show the learner how to do something interesting. Games are an obvious example, but they can get a bit much - how many different versions of Hangman do people want to create? Simple web apps are also an option with many programming languages. If something needs to be more utilitarian, then if possible it should be genuinely useful for solving a problem (the programmer doesn’t necessarily need to have this problem, they just need to see how to create a program to fix it). Frivolous little scripts that do things like recite “99 Bottles of Beer” to demonstrate for loops have their place, but that place is near the start only - by the end a programmer wants to be able to write useful programs.
  • Good exercises to stretch the reader- Many textbooks will have additional exercises for the reader at the end of each chapter that allow them to practice their skills and ensure they aren’t just copying a listing, but are genuinely capable of writing code from scratch in the language. These are effectively the “homework” assignments, and I’ve found that these can be far more important at teaching me how to use the language well for actual programming projects than the listings within the book.

These are my thoughts, but I’d be interested to read what other people think about this issue. What’s the best programming textbook you’ve ever used, and why do you like it? What do you think a good programming textbook should have?

16th August 2010 9:50 pm

Perl After Python

I’m currently studying for the CIW Web Developer qualification, and having passed the exams for database design and JavaScript, I’m now on to the third component, Perl. I figured that having already picked up a reasonable grasp of another scripting language (namely Python), that I would have no trouble picking up Perl quickly, as happened when I learned JavaScript.

Unfortunately, it hasn’t quite worked out as well as I’d hoped so far, and in a number of ways. First of all, it doesn’t seem to “fit your brain” quite as easily as Python does - I find that the significant number of non-alphanumeric characters used makes it less intuitive than Python, at least for me. I’m also not a great fan of the syntax - in particular, I really am not keen on the syntax used for object-oriented programming. In general I’m finding it a struggle to pick up many things I learned quite quickly in Python.

That said, Perl has plenty of awesome features. CPAN has a staggering number of modules available, and makes it very easy to install them. And of course, its support for regular expressions is second to none. Don’t get me wrong, it’s a language I really want to know better and be able to use well, but I am finding it quite hard going compared to Python.

I strongly suspect, however, that it may well be, at least in part, because I learned Python first and my brain is used to the Pythonic way of doing things, therefore I’m having to unlearn those habits for Perl. Has anyone else learned Python first and then struggled to pick up Perl, or is it just me? Does learning Python first predispose you to finding Perl more difficult?

Recent Posts

Forcing SSL in Codeigniter

Logging to the ELK Stack With Laravel

Full-text Search With Mariadb

Building a Letter Classifier in PHP With Tesseract OCR and PHP ML

Console Applications With the Symfony Console Component

About me

I'm a web and mobile app developer based in Norfolk. My skillset includes Python, PHP and Javascript, and I have extensive experience working with CodeIgniter, Laravel, Django, Phonegap and Angular.js.