Tag Archives: marketing

Learning from Bad Code

I’ve written before about Linux Format’s habit of sharing badly written Perl code. I thought things were improving, but in the new edition (November 2012, issue 163) they’re back to their old tricks.

This time it’s a tutorial called “Starfield: Learn new languages”. In this tutorial Mike Saunders writes similar starfield simulation code in C, Python and Perl. Mike’s loyalties are made perfectly clear when these three sections are entitled “Low Level”, “High Level” and “Unusual Level” respectively, so I wasn’t expecting much. But here’s what I got.

#!/usr/bin/perl

$numstars = 100;

use Time::HiRes qw(usleep);
use Curses;
$screen = new Curses;
noecho;
curs_set(0);

for ($i = 0; $i < $numstars ; $i++) {
  $star_x[$i] = rand(80);
  $star_y[$i] = rand(24);
  $star_s[$i] = rand(4) + 1;
}

while (1) {
  $screen->clear;

  for ($i = 0; $i < $numstars ; $i++) {
    $star_x[$i] -= $star_s[$i];
    if ($star_x[$i] < 0) {
      $star_x[$i] = 80;
    }

    $screen->addch($star_y[$i], $star_x[$i], ".");
  }

  $screen->refresh;
  usleep 50000;
}

Let’s be clear here. This code works exactly as Mike intended it to. But if you’re writing sample code for other people to learn from, I think you should be setting the bar a little higher than “works for me”. I think you should should be aiming to show people good quality code that can be easily mainitained. And this code falls well short of that aim.

Let’s look at some of the problems in more detail:

  • No use strict or use warnings. Let’s be generous and assume the editor removed them to save space.
  • Undeclared variables. Of course. if the code contained use strict then the variables would all have had to be declared. Not declaring them means that we’re using package variables rather than lexical variables. In code this simple it doesn’t make a difference. But it’s a bad habit to get into.
  • Indirect object notation. When creating the Curses object the tutorial uses the syntax $screen = new Curses. Again, not a problem in this program, but a really bad habit to be encouraging. In the article’s defence, the documentation for the Curses module only includes this flawed syntax.
  • Split data structures. The author of the article says “instead of having an array of stars containing coordinates and speeds for each one (i.e. an array of arrays), to make things simpler we’ve just set up three arrays.” I read this to mean “I’ve never been able to work out how to make arrays of arrays work in Perl so I’ve taken the easy way out.” This is, of course, a terrible idea. Linked data items should be stored in the same data structure.
  • C-style for loops. The mark of a C programmer who never really got to grips with Perl. The C-style for loop is rarely used in Perl code. The foreach loop almost always leads to more readable code.
  • Magic numbers. The size of the screen and the maximum speed of the stars appear as numbers in the code. Even if you’re not a Perl programmer, surely you would know that it’s good practice to move those into variables or constants.

With all that in mind, here’s my version of the program.

#!/usr/bin/perl

use strict;
use warnings;

use Time::HiRes qw(usleep);
use Curses;

my $numstars = 100;
my $screen_x = 80;
my $screen_y = 24;
my $max_speed = 4;

my $screen = Curses->new;
noecho;
curs_set(0);

my @stars;

foreach my $i (1 .. $numstars) {
  push @stars, {
    x => rand($screen_x),
    y => rand($screen_y),
    s => rand($max_speed) + 1,
  };
}

while (1) {
  $screen->clear;

  foreach my $star (@stars) {
    $star->{x} -= $star->{s};
    $star->{x} = $screen_x if $star->{x} < 0;

    $screen->addch($star->{y}, $star->{x}, '.');
  }

  $screen->refresh;
  usleep 50000;
}

What do you think? Is it more readable? Easier to maintain? Are there any problems or improvements that I’ve missed?

Google Currents

Google Currents is an application for viewing content on Android and iOS devices. It reformats content (based on web feeds) to look like a magazine. It looks great on my HTC One X and I’m expecting it to look even better on my Nexus 7 when it arrives.

It’s possible to subscribe to web feeds using it, but for some reason content looks better if the web site owner goes through a simple process to publish the content. This is literally a three minute job which you do from the Google Currents Producer web page. I’ve done that for both this site and the Perl News web site. I can’t seem to find any way to share the address of this content, but if you open the Currents app on your tablet or phone and search for “Perl Hacks” and “Perl News” you should be able to find them.

Currently I’m using all the default formatting options, but I suspect I’ll be drawn into tweaking things soon.

If you published Perl-related content on the web (or, indeed, any other kind of content) then it might well be worth your while taking the three minutes it takes to publish your content for Google Current.

More Modern Perl in Linux Format

Yesterday’s post bought my subscription copy of Linux Format issue 153. This issue contains the second article in my short series about Modern Perl. In this article we take the simple DBIx::Class application that we wrote last time and put a web front end on it using Dancer.

Over the next few days I’ll be writing the third (and final) article in the series. This will involve adding more features to the web app.

If the series is successful (and please let LXF know if you liked it) then perhaps I’ll be asked back to write more next year.

LXF 153 should be appearing in all good newsagents next week.

Doomed Domains

Summer is YAPC time. And YAPC means getting inspired on Perl-related projects. And that, obviously, means buying domain names for those projects. And that, inevitably, leads to lots of email from domain registries at about this time of year which roughly translate to “are you ever going to do anything useful with that domain you bought a couple of years ago, or should you just face facts and let it go?”

This year’s batch brings memories of projects from the last three years.

In Copenhagen in 2008 I gave a talk called Proud to Use Perl. To back it up I started a blog where I planned to share things that made me proud to use Perl. It didn’t last long. Even when I brought a team to help me, no-one had the time and nothing has been written there for two years. An advocacy site like that does more harm than good unless it it kept updated. So unless someone wants to take over the site (and keep it up to date) I’m going to let the domain lapse.

Lisbon in 2009 seemed to be largely about getting the Perl marketing project up and running. It was the scene of the famous Perl Marketing BOF. One of the ideas that came out of it was that Perl needed better web sites. I registered perlfive.com and perlfive.org in order to… well… I’m not really sure what they were for. Currently they both just redirect to perl.org. Do you have a better use for them?

And then last year in Pisa we had Perl Vogue. I was learning by that point and only registered the domain for a year. I’d really love for the Perl Vogue idea to really take off, but I’m not going to be the one to do it. If you want to try, then let me know.

Most of these domains expire some time in July. If you have ideas of what we can do with them then please get in touch. But, be aware that any suggestions that start “couldn’t you just…” are likely to be ignored. I’m looking for suggestions that start “I’d like too…”.

An Open Letter to Linux Format

Back in September 2010 I wrote a piece criticising the way that Perl had been described in a recent issue of Linux Format. In a response to my article, the editor wrote:

And, believe it or not, I do care about Perl. I met Paul Fenwick a few months ago, and he agreed to write a Perl tutorial series for us – I hope you agree with my choice!

So when I saw that a new series of Perl tutorials was starting in the “Coding Academy” supplement of the new issue (#149, June 2011) I was looking forward to reading a useful introduction to the language written by Paul Fenwick.

Unfortunately, I was disappointed. The tutorial was written by Marco Fioretti and, to be honest, it really wasn’t very good. It failed on three fronts.

  • The English wasn’t very good
  • The explanations of Perl were, in places, rather out of date or just plain wrong
  • The sample Perl code included some rather outdated practices

Let’s look at those three areas in more detail.

English Language

Obviously, English isn’t Marco’s first language. And his English is far better than my Italian. But I’d like to think that if I was writing for an Italian magazine, then the editors would tidy up my writing so that I didn’t make myself look too foolish. It appears to me that the Linux Format editors did nothing to correct Marco’s English. Here are a few examples of infelicities that slipped through.

“Before looking at the code, however, let me explain which data it uses…”
“Perl arrays and hashes are much more flexible than it shows in these pages…”
“The complete code of the procedures is in the DVD”

Of course, there’s nothing terrible there. We know what Marco meant. But the magazine would have looked a lot more professional if the editors had taken the time to edit this article.

Misunderstanding Perl

Marco’s knowledge of Perl seems a little mixed up or out of date in places. For example, he says:

You call a subroutine by adding a & to its name and passing parameters

It’s been several versions of Perl since the & has been required on a call to a subroutine. You won’t see it in any modern Perl code. It just makes the code look unnecessarily messy. Later on in the article, Marco says:

Perl does have while, if, for and unless, but it lacks a C-like switch operator for multiple, mutually exclusive choices.

The Switch module was added to Perl in version 5.8.0 (in July 2002). A much improved version called given/when was introduced in version 5.10.0 (in December 2007). Anyone who thinks that Perl doesn’t have a switch statement simply hasn’t been keeping up.

Outdated Perl

The Perl idioms that Marco uses are often somewhat outdated. In particular he uses the two-argument version of open instead of the safer three-argument version. He also uses global filehandles instead of lexical ones.

Marco also has a rather distinctive style for his Perl code, much of which runs counter to the advice in the perlstyle manual page or the Perl Best Practices book. In particular, his use of capital letters for variable and subroutine names will look very strange to someone used to reading Perl code formatted in a more traditional manner.

All in all, I think that Marco’s style does nothing to counter the reputation that Perl has for encouraging unreadable code and that with a little thought the same program could have been written in a far more readable manner.

Marco’s attitude to keeping up to date with Perl is nicely demonstrated by the books he recommends. “Programming Perl” and “The Perl Cookbook” were last updated in 2000 and 19982003. He would have been far better off recommending “Learning Perl” (which does keep up to date – the fifth edition was in 2008 and the sixth is currently in progress) or Modern Perl.

It’s a shame that the promised series of articles by Paul Fenwick hasn’t materialised. But it’s even more of a shame that Linux Format has fallen back to using articles written by someone who is not interested in keeping up to date with modern Perl. In light of this disappointment, I’d like to make the following offer to Linux Format.

If you want someone to write an article about Perl (whether that’s a one-off article about a particular topic or a series of tutorials), if you get in touch with me then I will find the best person to write that article for you. I may even do it myself if I have the time and the knowledge. Further to that, I am more than happy to proof-read and edit any Perl articles that you have currently in the pipeline.

I don’t believe that the Perl community in the UK is particularly difficult to track down. but if I’m wrong then I’m quite prepared to the the conduit that people to use to contact the community.

[I've found the sample program from the tutorial on the CD and have put it online.]

Marketing Perl

Sometimes people ask me why Perl marketing is so important. This morning I came across an excellent example of the kind of thing that we’re trying to counter.

In the current issue of Linux Format, there’s an article about building a Twitter client in the bash shell. It’s written by Nick Veitch – who seems to dislike Perl a bit. In the article he wants to URL encode a string and can’t find an easy way to do it in bash. He writes:

However, the compromise I’ve found for this isn’t too bad (props to http://stakface.com/nuggets). It uses Perl and, like everything in that language, it looks like you ought to sacrifice a goat or something before you run it.

perl -p -e 's/([^A-Za-z0-9.-~])/sprintf("%%%02X", ord($1))/seg'

It’s not clear to me which part of the Perl make him want to start sacrificing goats. Is it the s/// syntax that Perl borrowed from sed? Perhaps it’s the regular expression syntax that Perl shares with pretty much any language with regex support. Or maybe it’s the sprintf function that Perl borrowed from C and that many other languages (including bash) support with a pretty similar syntax.

Of course I’m not saying that Perl doesn’t have some grungy corners in its syntax. But the three pieces of syntax used in this code fragment look to me like things that should be understood easily by just about anyone with experience of programming in the Unix environment.

And if this code is so hard to understand, why use it? I don’t know what Nick’s programming language of choice is, but couldn’t you do exactly the same thing in Python, Ruby or PHP? I can’t believe that Perl is the only language that is so powerful. And I’d be very surprised if the same code didn’t look very similar in many other languages.

So why the unnecessary little dig at Perl?

Of course, it’s clear that Perl isn’t a language that Nick is particularly familiar with (neither, indeed, is the person who he took the solution from). Anyone who knew Perl would realise that Perl’s standard distribution includes the CGI module which contains an escape function which does exactly what Nick wanted – without exposing the programmer to all of that scary syntax. I would write Nick’s code something like this:

perl -MCGI=escape -e'print escape "@ARGV"'

There are a lot of people out there with really strange ideas about Perl. People who don’t bother to find out the best ways to do things in Perl. And having widely-read Linux magazines printing snide comments about Perl does no-one any good at all.

This is why I think that Perl marketing is important. We need to reach the people outside of the echo chamber and tell them that Perl isn’t the outdated, hard-to-use language that they are being told that it is.

Update: I should have pointed out that I’ve sent an abbreviated version of this to Linux Format. Hopefully it’ll be published in the next issue. And I’m considering proposing a series of articles on Perl to them.

Marketing Perl at FOSDEM

It’s two weeks since I went to FOSDEM and I promised to write an article about what happened there. Better do that before I forget everything.

Some time ago, Gabor applied for a Perl stand at this year’s FOSDEM. The idea was that we could go along and promote Perl to people who are part of the Open Source community but not part of the Perl community.

When I first arrived at the venue, it took me some time to find the Perl stand. This was largely because I was searching in the wrong building. I forgot that FOSDEM is spread over several buildings at the ULB. I had assumed that we’d be in the main building, but we were actually in another building along with most of the stands.

The Perl Foundation had paid for some stuff for us to give away from the stand. We had some postcards listing Perl events in Europe this year and some round tuits. There were also a few other leaflets promoting particular Perl events.

I think it was unusual for a programming language to have a stand at the conference. Plenty of other projects had stands, but I didn’t see any other languages. A lot of the other stands were promoting projects that they were able to demonstrate. I think it’s hard to demonstrate a programming language in a situation like that.

We got a lot of people passing by the stand and many of them stopped to talk. The round tuits attracted the most attention, but it was sometimes hard to explain the joke to people whose first language wasn’t English. There were at least a couple of times when I just gave up trying.

On Saturday afternoon, Juerd arrived. He brought a projector with him and we set that up projecting a hastily assembled slideshow on the wall opposite us. That also drew a lot of attention to the stand. In the future I think it’s a good idea to plan something like that in advance.

Just about everyone who we talked to knew about Perl. And most of them had used it at some point. Most of the people I spoke to were still using it to some extent. But very few of them knew about the “Modern Perl” projects that we were promoting (Catalyst, Moose, DBIx::Class, etc) or the huge number of Perl events that take place i Europe every year. I think we got some of them interesting in Modern Perl and I’m hoping that we’ll see a few new faces at various Perl events this summer. I promised to buy a drink for some of them if they come along to YAPC::Europe. If they all take me up on it, it might get a bit expensive.

Our presence at the conference was all very experimental. We know that this is something that we want to do more of, but we’re just working out the most effective approaches to take. But I think that we can count this attempt as a success and take the lessons learned forward to other non-Perl conferences. The next one on the list is CeBIT.

Other people have also written about this event: Gabor, Claudio, Erik, Salve.

The “Without Whom”s

In The “M” Word, I listed many of the things that have happened in the Perl marketing world over the last year. I wanted to end the year by mentioning a few people without whom this project would not have got off the ground at all.

  • Curtis “Ovid” Poe started writing about Perl’s image issues on his use.perl journal back in July. But he didn’t just write about it. He chaired the Marketing BOF that we held in Lisbon and he was the driving force behind setting up the TPF marketing committee. He has also been instrumental in getting the blogs.perl.org site up and running.
  • Gábor Szabó wasn’t very far behind Ovid in starting to blog about marketing. He has also been vociferous in TPF marketing committee discussions. He always has another idea for projects we can take on and often does those projects himself before anyone else has a chance to volunteer. I particularly want to draw attention to the clean-up he did on pm.org (a site full of links to dead Perl Monger groups isn’t great advertising) and his work trying to increase Perl’s visibility at non-Perl conferences.
  • Matt Trout and Mark Keating set up the Iron Man Blogging competition. To my mind this has been the single biggest success in marketing Perl this year. I’m still astonished daily by the number of people out there who are blogging about Perl. They have somewhere around two hundred blogs in the competition.
  • Leo Lapworth has done some amazing work dragging Perl’s web sites into the 21st century. Not many weeks go by without Leo announcing that he has redesigned another web site. I’m sure I’ve forgotten some of his announcements, but for a sample of his work see www.perl.org, learn.perl.org, dbi.perl.org and lists.perl.org. Thanks should also go to his employers, Foxtons, who donated some of his time (and some time from a web designer too).

Of course it’s true that the recent resurgence of Perl is due to the new and improved modules that you can find on CPAN. Moose, DBIx::Class, Catalyst and Plack (to name only four) are what enables Perl to retain its place in the forefront of modern programming languages. But it’s the people listed above (and dozens of other who also work on marketing projects) who enable Perl news to break out of the echo chamber.

We owe these people our thanks. And, on a more practical level, we owe them beer. If you see them at a Perl Mongers meeting, at a conference or even just on the street, it is your duty to stop them and say the magic words:

“Hi. Thanks for your work. Can I buy you a beer?”

The “M” Word

Yesterday was the London Perl Workshop. As always it was a fabulous day packed full of great talks about Perl. Thanks to the organisers for all the work they put in.

I gave the keynote speech first thing in the morning. The talk was called The “M” Word and it was an overview of how the Perl community has started to get to grips with the problem of marketing over the last year.

Here are the slides:

YAPC::Europe 2009

Today I travelled home from YAPC::Europe 2009 which was held in Lisbon. Readers of my other blog will know that I almost didn’t get there at all. The return journey was far less stressful.

On the weekend before the conference I gave a two-day “Introduction to Perl” course. We had six people on the course. I’m pretty happy with that as I think it’s a brave decision to have a beginners’ course at a Perl conference. I’m also happy to report that a third of the attendees were women.

There were four tracks of conference talks. This, of course, is guaranteed to lead to situations where you want to watch more than one talk at the same time. I think that at one point I really wanted to watch three of the four talks. Of course, there’s also the “hallway track” which is the best part of any good conference. This week there were two or three occasions when I found that I’d missed talks that I wanted to see because I had been so engrossed in an interesting conversation. All in all, I’m really glad that some of the talks were being recorded.

One highlight for me was meeting Paul Fenwick and Jacinta Richardson. I’ve been talking online to Paul and Jacinta for something like seven or eight years but we had never met face to face before because we spend most of out lives on opposite sides of the Earth. It was great to finally meet them. They’re as lovely in real life as they are online and they’re also both great speakers with interesting things to say.

Another highlight was the Marketing BOF that we held on Tuesday evening. There’s a growing concern in the Perl community that people outside of the community have an out of date and rather uncomplimentary view of Perl. The conference was full of people who had many ideas for fixing this. Ovid has a good description of this BOF over on use.perl so I won’t repeat the details here. I’ll just point out that some of us tried to have a similar meeting at EuroOSCON in 2005. At that time we had four people turn up. This week there were more than fifty.

The quiz show on Tuesday night was fun too. Greg and I were a last minute substitution as one of the teams failed to appear. We won our qualifying round, but came third (out of four) in the final. I was particularly embarrassed to have been beaten to the answer to the Buffy question.

And it seems that I owe the Perl community an apology. In his closing keynote, José Castro gave some examples of bad Perl advocacy. Number one on his list was my Why Corporates Hate Perl article. José pointed out that many people only read the title of an article and that my title was, perhaps, badly chosen. I’m sorry if I’ve caused any damage.

All in all, a great conference. Many thanks to the organisers for all of their hard work.

Hope to see you all in Pisa next year.