Stop with the Perl golf already!

It's annoying to read critics complaining that  "Perl looks like line noise" since that's definitely not my experience... at least, most of the time.

There are some places where I think Perl actually does look a bit "noisy":

  • sigils ($@%&), but only when most variables have single-letter names ($i,$j,$k, etc.)
  • punctuation-only special variables ($/, $], etc.), but I find these are used infrequently in most "real" code I write or see
  • explicit dereferencing (@{$foo->{bar}})
  • regular expressions, but this is hardly Perl's fault

But when I think about where the reputation for line noise comes from, the thing that jumps out at me is the Perl (sub)culture that turned code golf and obfuscation into a source of pride -- a demonstration of high art and coding wizardry.

Think about it.  IBM has a technical paper on JAPH's and describes them as "a mainstay of the Perl culture".  Obfuscated perl shows up on tee-shirts.  Perl Monks has an entire section on Obfuscated Perl.   The sigils themselves even became the logo on the official shirt for YAPC::NA 2008 as a visual pun on the Chicago city flag.

So, of course, people are going to equate Perl with line noise.

And who am I to be throwing stones?  I've done it too.

I recently gave an answer on StackOverflow to the question, "What's the shortest perl program you can write for the number guessing game?" That question and the subsequent answers were  the genesis of this article.

I had the idea to show how the guessing game could be played recursively to golf by avoiding variables to hold guesses.  Just for the sake of argument, if I were writing it out for someone to understand it, here's how I might do it:

use strict;
use warnings;
my $target = 1 + int( rand(10) );
my @message = ('', "too much\n", "too few\n");

sub guess {
  print "Gimme your guess 1-10:\n";
  my $guess = <STDIN>;
  my $comparision = $guess <=> $target;
  if ( $comparison ) {
    print $message[$comparison];
    return guess();
  else {
    return $target;

my $answer = guess();
print "Bingo! It was $answer\n";

And here's the short version I came up with:

($t,@m)=(1+int rand 10,'',"too much\n","too few\n");sub g{print
"Gimme your guess 1-10:\n";syswrite(STDOUT,$m[<><=>$t])?g():$t;}
print"Bingo! It was @{[g()]}\n"

That's not even that bad as obfuscation goes, but I looked to the two really nasty bits ( "<><=>$t" and "@{[g()]}") and though to myself, "Damn!  No wonder people complain about line noise!"

It reminds me of a quote about discrmination by the US chief justice.  Adapted to this context, it means the way to stop people thinking that Perl looks like line noise is to stop Perl from looking like line noise.

That's my new pledge.  No more Perl golf and no more JAPHs for me.  The Perl I'll take pride in is Perl that people can read.

This entry was posted in perl programming and tagged . Bookmark the permalink. Both comments and trackbacks are currently closed.


  1. Posted June 9, 2009 at 12:43 am | Permalink

    Actually It think the bad reputation is more from code that was written badly
    by people who don't know Perl well enough

    * lots of short variable names such as $tem1$temp2 $temp3)
    * extensive unnecessary escapes: print "this \"is\" really bad"
    * also in regexes /this\/needs to \= \'match\'\//

    and then left like that for years because managers neither require the improvement of old code nor give you time to do that and actually they
    prefer the culture of "what's working we don't change"

    • david
      Posted June 9, 2009 at 5:39 am | Permalink

      The first two are the exclusive province of Perl, though, so while a reputation for bad code might result, I don't see how someone gets from those to "line noise". Even if "line noise" as an epithet is an exaggeration of what people see day to day, I think it sticks because it reminds people of more extreme examples they've seen. (But I do agree on the regex point.)

  2. Posted June 9, 2009 at 2:30 am | Permalink

    I am confused as to why you want to deprive yourself of a fun activity (Perl golf), just because some non-Perl-programmers dislike it. Who cares what they think?

  3. Posted June 9, 2009 at 5:19 am | Permalink

    By the same argument, F1 racers should stop having fun with go-carts in their spare time, and Tiger Woods should be strictly banned of ever trying miniput. Oh, and any software engineer looking at the Intercal manual should be apprehended and shipped to an island in the arctic sea. And people using the English language to make funny lolcat captions should be shot on the spot.

    I can understand the tactical good sense of not showing a piece of Perl golf as a first introduction to the language to a newbie. But not having fun with the language because it might reflect bad on it? It's quite silly.

    • david
      Posted June 9, 2009 at 5:36 am | Permalink

      F1 and Tiger Woods are bad analogies because (a) those pastimes are simpler forms of the sport and (b) neither sport has a reputation for resembling the pastime. Intercal and lolcat are bad analogies mostly because you suggest extreme outcomes, whereas I'm simply suggesting refraining from a practice. For example, I'd have no problem if people just stopped writing lolcat captions and I do think that people who write them excessively (or start writing like lolcats in unrelated messages) are more than a little odd.

  4. Posted June 9, 2009 at 6:08 am | Permalink

    And anyway, ${\g()} is shorter ...


  5. dan
    Posted June 9, 2009 at 7:01 am | Permalink

    I think you're wrong about the perception that Perl is "executable line noise". The only group of people I have seen raise that as a serious argument (other than in jest) are other Perl programmers.

    When Python, Java, Ruby, C#, etc people complain about perl being "ugly", they're more likely referring to things they don't understand (sigils, s/// syntax, strange operators, lack of an explicit 'class' keyword, manual sub arg unpacking, push @foo instead of @foo.push, etc). None of these things are really important in the grand scheme, and they're doing it in exactly the same as people whining about Python's significant-whitespace. That is, it's seen as a problem because it's different, but in reality it really isn't that big a deal, and isn't likely to stop anyone from learning.

    If you want to convince newbies to try Perl out, show them the good bits. Show them CPAN, show them Moose, show them Catalyst. Attempting to hide "golf" from them really isn't very productive (as most will have never been exposed to it anyway).

    Fun fact: Ruby has Perl's punctuation variables (as well as a lot of Perl's other syntax constructs). There aren't many people who think Ruby looks ugly ;)

    • david
      Posted June 9, 2009 at 8:29 am | Permalink

      The serious arguments can (usually) be addressed through rational debate. Stereotyping can't.

      I'm all for showing the good bits, and try to make my own contributions to the good parts (e.g. Capture::Tiny, which is probably my best attempt so far). I'm also in favor of a more aggressive schedule for improvements in the Perl core to address what ugliness can be fixed while remaining recognizably Perl 5.

      I'm not talking about "hiding" golf -- but I'm saying that time spent golfing is time not spent showing people the good bits or showing people the clearest, most expressive way to do things.

  6. nyet
    Posted June 9, 2009 at 9:10 am | Permalink

    I don't think Perl golf is a harmful activity, if you do it outside production code

  7. Bill
    Posted June 9, 2009 at 9:27 am | Permalink

    I think we should all just not consider Perl Golf or JAPHs to be "Perl." Instead just consider them as they are: games.

    In effect what you are doing is gaming perl.

  8. Posted June 9, 2009 at 11:58 am | Permalink

    Good call in general :)

    Of the points you make - this one stands out to me, as something I actually use fairly often in anger:

    explicit dereferencing (@{$foo->{bar}})

    flatten method.

  9. eyepopslikeamosquito
    Posted June 9, 2009 at 2:38 pm | Permalink

    During the past couple of years, golf, as a competitive activity, has moved on and is nowadays played in more than 50 languages. As mentioned in The golf course looks great, my swing feels good, I like my chances (Part IV) competitive golf is now more popular in Python than Perl. The Python golf community is thriving, much more so than the Perl golf community!

  10. Posted June 9, 2009 at 5:20 pm | Permalink

    I actually don't think golf has *anything* to do with Perl's reputation as noisy. If anything it's its sysadmin applications: one-liners and such. And just plain FUD. Look at any PHP in the wild or even Ruby or Java. Few developers write clean, obvious, terse code in any language. Perl just was the first high level language to blanket the innertubes.

    So, maybe pick another windmill. :)

  11. eyepopslikeamosquito
    Posted June 9, 2009 at 7:04 pm | Permalink

    To clarify my previous post, while your arguments re Perl golf may have been reasonable in the past, recent developments in the non-Perl code golf community clearly demonstrate that golf is not specific to Perl. Do you expect Python to now be similarly slandered as "unreadable line noise" because of its strong and enthusiatic golf community? I doubt that will happen and hope that the wider programming community will finally accept that the readability of a programming language has nothing to do with the popularity of playing golf in it ... though it is perhaps too much to expect folks to accept that code "readability" depends on other factors much more than on language syntax. :) See also a related chromatic post.

  12. Ed
    Posted July 29, 2009 at 6:20 pm | Permalink

    The problem is not necessarily Perl golf itself, but winning programming golf hands-down with Perl. Python does not, to my knowledge, have the potential to injure its reputation with golf as much Perl does. Also, if people see things written in a language frequently, they're less likely to think badly of the language because of seeing a few bad examples.

    It could be that I'm out of touch, but I haven't seen anyone putting Python golf examples in their .sig files. I do see this with Perl, however. One of my former coworkers even had (or maybe has) a 30ish character piece where the only contiguous letters were in the first eight characters: "perl -ape".

    As far as the comment about only Perl programmers complaining about the line noise effect, that's completely contrary to my experience. I've heard it from several former Perl programmers, as the explanation of why they left, and I've heard it from students of a former Perl programmer, who explained they'd never learn Perl because it's just line noise. That last bit, that hurt. (Especially because I'd seen both Perl and Python code from their instructor, and my Perl code's been more legible than anything I've seen from him since back when he was doing Perl.)

    As far as other sources of the reputation for Perl line noise - yes, they're all sources. I realize we can't stop all of them, but we should stop every one we can.

  13. Posted August 3, 2009 at 1:22 pm | Permalink

    I strongly support the autor's attitude.

    non-Perl programmers opinion is critical, as when they are afraid of Perl, they don't learn it, and there are no new Perl programmers. We are just migrating from Perl to Python because Python programmers are easier to be found, and C++/Java/Net programmers are more eager to learn Python than Perl.

    And I've seen Perl golf examples shown as arguments on "pick-a-technology" meetings.

3 Trackbacks

  • [...] in Programming tagged advocacy, Perl at 5:01 pm by Mutant David Golden has called for an end to golf and obfu in an effort to dispel the “Perl is line noise” [...]

  • By MalDevBlog » Zmierzch Perl Golfa? on June 10, 2009 at 6:51 am

    [...] Jednym z najlepszych sposobów oczyszczenia wizerunku Perla, według Davida Goldena byłoby zaprzestanie zabaw z golfem i celowym zaśmiecaniem kodu, co próbował przedstawić na swoim blogu w wpisie Stop with the Perl golf already! [...]

  • [...] out, is because he looks sort of cross in his profile picture), David Golden thinks people should stop with the perl golf, and Sam Crawley thinks that people dislike Perl because they’ve had to deal with badly [...]