It seems that the past couple of days I’ve had to spend a fair bit of time working with programming languages beginning with the letter ‘p’:
At work last week, I wrote my first ever perl script. Our client had asked for a script which parsed a CSV file and inserted the data into a database table. I chose perl because python was unavailable (and I’m not sure how good bash scripts are at parsing CSV, I did google it but it didn’t come up with much useful info). Anyway, it seemed to go alright – perl seems to have a rather “quirky” syntax (i.e., it’s quite strange) but once you get used to it it’s not too bad. Plus, a lot of PHP seems to be based on perl so some bits already seemed quite familiar.
Perl does have a bad reputation in some circles, mainly because it is a very good language to do “quick and dirty” fixes in which fix the problem but are badly written and are a maintenance nightmare! I suspect that perl is more open to abuse in that respect. But it is perfectly possible to create decent code using it, and because it’s such a well-established language there is a large amount of documentation on the web for it.
I mentioned python as well at the start of this post: I said a few days ago that I had written a shell script to rip CDs and upload them to the Fordham website. Well, last week I converted the website to use a database – previously you had to browse the filesystem to find sermons, whereas now you can search for them! (It’s not complete just yet, but hopefully will be soon…)
Anyway, the only problem with the shell script was that there is no easy way to connect to a MySQL database using bash scripting (short of starting up the mysql client executables locally, but I didn’t want to do that!). Python has a nice library called MySQLdb which enables it to talk to MySQL databases.
I really don’t know why I didn’t write the upload script in Python in the first place… what I like about Python is that it’s a powerful programming language, and yet it’s just about as easy to use as a standard shell script. You can do object-oriented programming if you want to, but you can also just use it to write a quick script to rip a CD and upload it to a website… in other words, it’s very versatile and fast.
In fact, the only thing I don’t like about python is the fact that when you’re creating a class, all the methods have to take the parameter “self”. In Java, the identifier “this” refers to the instance of the class in which the code is currently executing. I’m not srue why python doesn’t do this… I noticed that perl uses the same technique (i.e. the instance of the object is passed as parameter), so perhaps it’s a feature of interpreted languages?
Anyway, just a minor gripe. Once you get used to it, it’s no problem at all!
*phew* – that turned out to be a longer post than I’d expected it to be! Sorry about that! All for now…
2 thoughts on “Perl and Python2 min read”
‘self’ is only a convention, it is not hard coded in the interpreter. If you want to use ‘this’ (or anything else for that matter) then it will work, as long as you are consistent.
Well, it’s not so much the name of the parameter – it’s just the fact that you have to have it as a parameter. In the languages I’m used to working in, ‘this’ is sort of an implicit variable.
Not that it really matters, it’s just a minor thing 🙂