Drawing Traffic Lights With Perl

Traffic Lights

For a thing (that you may hear more about at some point in the future) I needed diagrams of traffic lights. But Google Image Search didn’t really have what I was looking for. Everything was either too realistic or not CC-licensed so I could use the images how I wanted.

So I decided to do it myself. But I’m not exactly artistic. I far prefer it when I can get computers to draw images for me. I’ve dabbled with SVG before and it seemed like the perfect tool for the job. And there’s a module from CPAN that makes it simple to create SVG images from Perl.

It only took an hour or so before I was drawing images like the one above – which was exactly what I was looking for.

Initially, I shared my code as a Gist, but since then I’ve extracted the useful bits into a module which I’ve uploaded to CPAN as SVG::TrafficLight. I’ve tried to make it as configurable as possible, so you should be able to use it for all your traffic light drawing needs as well.

Starting to use it is pretty simple.

The default sequence of lights shows the UK’s standard traffic light sequence (green,  amber, red, red and amber, green) but it’s simple enough to produce a different sequence (even one that you would never see on the roads).

If you read the documentation, you’ll see how you can customise pretty much anything in the diagram – the size of the lights, the padding between them, even the colours used.

Let me know if you find it all at useful. SVG is fun. I’ll think I’ll investigate it some more.

 

I Wrote Some Perl

Ok, yes, I’ve been writing Perl for over twenty years. But Perl 5.26 was released this week and for the first time, my name is mentioned in the release notes. Because I have not one, but two fixes in this release of Perl.

The first is this commit which fixes a piece of documentation to make it clear that grep() returns a list, not an array.

The second is this commit which fixes some sample code so that it runs without warnings under use strict.

It’s a small start, I admit, but I have a taste for it now. In a years time, I hope to report that I have more than two commits in Perl 5.28.

And you can help too. Instructions on how to contribute are in the perlhack manual page. There is more information in perlhacktips and perlhacktut.

The people working on Perl all do a great job. But it’s a hard job and it might just get a little easier if more of us helped out.

Two Weekend Projects

It’s far too long since I’ve posted anything here. I’ve no excuse really. Following the end of my contract in Canary Wharf, I was off work for seven weeks. OK, I was on holiday for two of those weeks, but that still leaves five weeks when I could have been doing something constructive, but actually just spent a lot of time watching Netflix.

But there were a couple of things I did. Neither of them took more than a few hours, but I thought it was worth writing them up – if only to give an example of a couple of really useful (to me, at least) things that I was able to build really quickly with Perl.

Cooking Vinyl

If you were a music fan in the 1990s, then there’s a good chance that you own at least one album released on Cooking Vinyl Records. At times, it seemed like pretty much every album I bought was released by them. Back in 2005, I wrote a blog post where I tried to explain how much they meant to me.

In particular, they produced a series of compilation albums that introduced me to so many of my favourite acts. Ten years ago, I tried to find a definitive list of all of the songs and artists which appeared on those compilation albums. As I failed to find one, I created it myself. At the time, it was a static list of albums which listed the tracks and artists on each of the albums. For ten years I’ve had it in the back of my head to do something more interesting with the data. A few weeks ago, I finally got round to it.

As I said, the original page just had a list of albums with artists and song titles. That’s useful, but it would be more interesting to be able to cross-reference the data in various ways – list all of the albums that an artist appeared on, for example. And for that, we need a database.

If you’ve come on any of my database training course over the last ten years, you’ll know that I use a CD database example. The model that I use is pretty simple and, in particular, it assumes that all tracks on a given CD are by the same artist. As I say in the class “various artists compilations don’t exist in this simplified universe”. Obviously, that’s not going to work in this example. So I needed to come up with another database model.

Compilation album data model
Compilation album data model

Here’s the data model I designed. You’ll see that it all hinges on the track table. A track is an instance of a particular song, recorded by a particular album appearing on a particular album. The only extra data on the track table is the “number” column which allows us to declare the order in which tracks appear on an album.

Advanced students will have spotted an omission from the data model. An artist might well have different versions of a song. There could be the original version, an edited single version and many live or remixed versions. So actually, we could add a “recording” table and it’s the recording that appears on an album. That’s, perhaps, an enhancement for the future.

Having designed the database the rest of the code just falls out really. I already had a data file so it was just a case of parsing that and inserting the data into an SQLite database. DBIx:Class (and, particularly the find_or_create method) makes this trivial. I then wrote another program that generated the web site using the Template Toolkit. Nothing complex there at all.

The site is at http://cookingvinyl.dave.org.uk/. And all of the code is on Github. It could do with being made a bit prettier – perhaps I can add some pictures.

Why not have a look. And check out some Cooking Vinyl recordings.

Tower Bridge

I’ve lived in London for thirty-five years. And in all that time I have never seen Tower Bridge opening. Oh, I’ve seen it when it’s  open, but I’ve never been in the right place at the right time to see it actually opening. As a Londoner, that’s a matter of supreme embarrassment to me.

But the office I’m working in currently is three minutes walk from Tower Bridge. All I need is a way to get a notification a few minutes before the bridge lifts. Surely, there must be a way to get that?

Sadly, no. The Tower Bridge web site has a page listing the upcoming lifts, but no service that would send any kind of notification. So, once again, it was up to me to provide one. I asked the London Perl Mongers on IRC what would be a good way to get notifications of upcoming events on an Android phone and Ilmari pointed out that the obvious method was to create a calendar that could be read by the calendar app on my phone.

So that’s what I’ve done. I use Web::Query to scrape the data from the Tower Bridge web site (doing some over-complicated madness to account for the fact that they are missing the year from their dates) and then create a .ics file using Date::ICal and Data::ICal. I also create a JSON version of the data in case it’s useful to anyone (if it is, please let me know).

The site is at http://towerbridge.dave.org.uk/ and (of course) the code is on Github.


So, there you are. Two (hopefully) little projects that I threw togther in a very small amount of time using the power of Perl. Please let me know if you find either of them useful.

YAPC Europe 2016

I’ve been back from Cluj-Napoca for almost a week, so I should really write down what I remember about YAPC Europe before it’s all forgotten.

Day -1

I arrived in Cluj-Napoca on Sunday evening and got to my hotel quickly. There was just time for a quick meal before bed.

Monday was the day that I was going to get to grips with the city. After meeting a few Perl Mongers at breakfast, my wife and I set off to explore. My first target was to find Cluj Hub, the venue where I was running a training course the following day. That was simple and took less than fifteen minutes. We then explored both the Orthodox and Catholic cathedrals before settling into a bar on the main square called “Guevara” for a coffee. After that we decided that we needed to pick up some supplies and whilst on that hunt we bumped into Max Maischein who recommended a visit to the botanical gardens.

On returning to the hotel with our supplies, we met Curtis Poe and invited him to join us for lunch. Wandering at random we found a really good restaurant called Livada and enjoyed a very pleasant meal.

After lunch we spent a very enjoyable couple of hours in the botanical gardens and only just failed to get back to the hotel before it really started raining. That evening we ate in restaurant really close to the hotel called the Crying Monkey (but in Romanian).

Day 0

Tuesday was my “Modern Web Development with Perl and Dancer” training course. This was by far the most successful training course that I’ve ever run at a YAPC. I’ll write more about it when I get the feedback results, but I think that the attendees enjoyed it. I know I had great fun giving it. Cluj Hub was a great venue and Andra Gligor and her small team looked after us all really well.

That evening, the traditional pre-conference meet-up was held on the roof of Evozon’s offices. As always, it was lovely to catch-up with old friends that I only get to see once or twice a year.

Day 1

On Wednesday, I set off in plenty of time to find the venue. It turned out that our hotel was really well located for both sight-seeing and the conference and I got there in ten minutes or so. The registration queues seemed shorter than usual and before long I had my name-tag and bag of conference swag.

As always, there were far too many good talks and it was impossible to see everything. I’ll just talk about the talks that I saw. Everything was videoed, so it will all be online soon.

The day began with Amalia welcoming us to the conference. Then the YAPC Europe Foundation announced that next year’s conference will be in Amsterdam. This is the first time that the conference has returned to a previous city (the second YAPC Europe was held in Amsterdam back in 2001) and I’m looking forward to going.

The first day’s keynote was from Curtis Poe. It was a wide-ranging talk covering the history and future of both Perl and the Perl community. After that I went into one of the small rooms to see H. Merijn Brand talking about his recent improvements to Perl’s CSV parser followed by Alex Muntada on how the Debian project packages CPAN modules. I then went back to the main room to see Mickey Nasriachi talking about PONAPI, which is a Perl implementation of JSONAPI.

Lunch suffered slightly from the inevitable queues, but it was worth the wait as the quality of the food (as it was throughout the conference) was very high.

After lunch I saw Lee Johnson giving some Git tips, Sawyer talking about the XS guts of Ref::Util and Jose Luis Martinez talking about PAWS (the Perl interface to Amazon Web Services). I saw Jose Luis talking about PAWS last year in Granada but really wanted to see how the project is progressing. I think this has the potential to be a great advocacy tool for Perl.

A quick coffee break and then I saw Thomas Klausner give his opinions on writing API endpoints and Tina Müller talking about App::Spec which looks like a great tool for easily writing command line applications.

Then it was was lightning talks. They were the usual combination of the useful, the banal and the ridiculous. I think the highlight for me was Curtis Poe announcing more details of his online game (which is now officially called Tau Station). This was the point at which I announced Cultured Perl – which seems to be going well so far.

That evening was the conference dinner. Which was a buffet party held in the open-air quadrangle at the centre of the Banffy Palace (Cluj’s major art museum). A great time was had by all.

Day 2

Another day, another keynote. This time it was Sawyer X with “The State of the Velociraptor” – an annual round-up of what’s going on in the Perl 5 world. This year Sawyer found a number of volunteers who all gave short talks about their part of the Perl community. This was a great idea which was only slightly marred by the fact that the projector wasn’t at all happy changing laptops – so the switches between presenters weren’t as smooth as they could have been.

After that I saw Max talking about how he uses ElasticSearch on his laptop to give himself a local search engine and Job van Achterberg talking about making web sites more accessible. This was a great talk – particularly the sections where he showed just how bad most web sites appear to screen readers.

Another queue for another great lunch. And also many interesting conversations.

After lunch I saw a former colleague, Mirela Iclodean, talking about how her company have managed to shoe-horn many modern tools and practices into their working day – while still maintaining a nasty monolithic code-base which they are slowly chipping away at. It was a great talk and it made me miss working on that project. I’m hoping that she will repeat this talk at the London Perl Workshop.

Later that afternoon, I gave my “Error(s) Free Programming” talk – in a slot where every speaker was a London.pm leader. The talk seemed to go down well, but somehow I ran considerably short.

After that I saw Albert Hilazo talk about his first few months as a Perl programmer. I found this really interesting as Albert talked in some detail about things that other language communities provide but he found hard to find for Perl. In particular, he would like to see more “war story” blog posts showing how people have solved particular problems using Perl tools.

Then it was Matt Trout celebrating ten years in the Perl community by explaining how his career was largely a series of happy accidents and that a lot of the responsibilities he has taken on were just through being in the wrong place at the wrong time – or something like that.

One talk I couldn’t miss was Andrew Yates talking about the work that his team do at the European Bioinformatics Institute. I couldn’t miss it as I was at least partly responsible for Andrew proposing the talk. I ran some training at the EBI earlier this year and during our email conversation YAPC was mentioned and Andrew asked if people might be interested in hearing about their work. I replied “hell, yes!” and sent him a link to the talk proposal web page.

And then, of course, there another ten or so lightning talks to close the day entertainingly.

Day 3

The keynote speaker on the last day was Larry Wall. His topic was “Strange Consistency”. If you’ve seen Larry speak before, you’ll know what it was like.

I followed that by watching Jason Clifford talk about how his team had written a major new toolset in Perl despite management pressure to use other technologies. The project, of course, ended up being very successful.

One of the most interesting talks was Nicholas Clark’s view of an alternative universe where Jon Orwant never threw those mugs in 2000 and the Perl 6 project was never started. The main lesson appeared to be “what goes around, comes around” and his fictional universe didn’t end up too far away from where we are now.

The afternoon had a curious combination of some time slots where I wanted to see every talk and others where I didn’t really want to see anything. So in some cases I’m eagerly awaiting the videos going online and in others I sat in the back of the room only half-concentrating while giving most of my attention to Twitter or Facebook.

I really enjoyed Sawyer talking about the things that were added in Perl 5.24 (and very carefully not talking about the things that were added in previous versions) and also Jose Luis Perez talking about what he has got out of doing the CPAN Pull Request Challenge.

The final lightning talks were as much fun as they always are. The projectors were still giving the speakers plenty of technical difficulties which led to lots of time for “lightning adverts” between the talks. I think that towards the end the differences between the two rather broke down and on the video at one point I expect you’ll hear Geoff Avery saying “I seem to have lost control of this”.

The conference ended, as it always does, with a brief presentation from the organisers of next year’s conference, a final thank-you to all of the speakers and sponsors and a standing ovation for the organisers.

This was one of the best-organised YAPCs I’ve been to for a very long time. And Cluj-Napoca is a city I would never have considered visiting if it wasn’t for the Perl community there. And already I’m considering a return visit. I had a lovely time in the city and returned to London completely recharged and reinvigorated.

See you all in Amsterdam next year.

Cultured Perl

Back in about 2008, I set up a group blog called “Cultured Perl”. The idea was to have a blog that concentrated on the Perl community rather than the technical aspects that most Perl bloggers write about most of the time. It didn’t last very long though and after a few posts it quietly died. But the name “Cultured Perl” still appeals to my love of bad puns and I knew I would reuse it at some point.

At YAPC Europe 2010 in Pisa, I gave a lightning talk called Perl Vogue. It talked about the way the Perl modules come into fashion and often go out of fashion again very quickly. I suggested an online Perl magazine which would tell people which modules were fashionable each month. It was a joke, of course (not least because Vogue are famously defensive of their brand.

Over the last many years people have suggested that the Perl community needs to get “out of the echo chamber” and talk to people who aren’t part of the community. For example, instead of posting and answering Perl questions on a Perl-specific web site like Perl Monks, it’s better to do it on a general programming site like Stack Overflow.

Hold those three thoughts. “Cultured Perl”, online Perl magazine, getting out of the echo chamber.

Medium is a very popular blogging site. Many people have moved their blogging there and it’s a great community for writing, sharing and recommending long-form writing. I get a “recommended reading” email from Medium every day and it always contains links to several interesting articles.

Medium has two other features that interest me. Firstly, you can tag posts. So if you write a post about web development using Perl and tag it with “web dev” then it will be seen by anyone who is following the web dev tag. That’s breaking out of the echo chamber.

Secondly, Medium has “publications”. That is, you can bring a set of articles together under your own banner. Publication owners can style their front page in various ways to differentiate it from Medium’s default styling. Readers can subscribe to publications and they will then be notified of every article published in that publication. That’s an online magazine.

So I’ve set up a publication on Medium (called “Cultured Perl” – to complete the set of three ideas). My plan is to publish (or republish) top quality Perl articles so we slowly build a brand outside of the echo chamber where people know they can find all that is best in Perl writing.

If you write about Perl, please consider signing up to Medium, becoming a contributor to Cultured Perl and submitting your articles for publication. I’ll publish the best ones (and, hopefully, work with authors to improve the others so they are good enough to publish).

I’m happy to republish stuff from your other blogs. I’m not suggesting that we suddenly move all Perl blogging to Medium. For example, whenever I publish something on Perl Hacks, the post gets mirrored to a Perl Hacks publication that I set up on Medium earlier this year. There’s a WordPress to Medium plugin that does that automatically for me. There may well be similar tools for other blogging platforms (if you can’t find one for your blog – then Medium has an API so you could  write one).

If you are a reader, then please consider subscribing to Cultured Perl. And please recommend (by clicking on the heart symbol) any articles that you enjoy. The more recommendations that an article gets, the more likely it becomes that Medium will recommend it to other readers.

I have no idea how this will go, but over the next few months I hope to start by publishing four or five articles every week. Perhaps you could start by submitting articles about what a great time you had at YAPC Europe.

Oh, and here are the slides from the lightning talk I used to announce this project at YAPC Europe in Cluj-Napoca, Romania yesterday.