Archive - Originally posted on "The Horse's Mouth" - 2013-05-20 23:14:49 - Graham Ellis
For short programs, just a few lines of code in a single block do an excellent job. As the program size increases, some sort of structure such as named blocks of code (sub, def, function, proc, lambda etc) becomes logical. And as the code grows further, you should consider splitting out logic relating to a particular data type into a class of methods, with each class having its own test harness.
There's a parallel here in real life writing - a tweet is a single block. Posts to social media such as facebook will be a series of paragraphs. And technical articles will be a series of linked pages.
Python - being an object oriented language - is designed around the larger coding model. And that larger coding model is very common in these days of shared code between a lot of smaller projects; that's why Python is especially popular. But you can also write excellent Object Oriented code in Perl to conform to the same principles, and give you the same benefits of unit testability and code re-usability. And that's something we cover on our Perl for Larger Projects course which is running this week.
Yesterday, we covered OO as implemented in Perl, statring from first principles. And during the day I built up to a complete example - [here] - showing object definition and inheritance, and moving on to look at patterns for generic property accessors through get and set methods, and also through the autoloader. The example also includes factory and comparator design patterns which you're welcome to look at, copy, adopt... or come on the course next time to learn in more depth.