Perl Golf

A game where one attempts to write the shortest Perl program to accomplish some goal.

In theory this game could be played in other languages, but PerlLanguage is particularly well suited to it due to the ample choice of operators and control structures, mostly based on punctuation rather than bulky keywords, many of which are completely different in meaning but very similar in appearance.

JavaGolf seems too absurd to contemplate.

JavaLanguage has a promising core of terse and pliable C-like syntax buried in a lot of keyword based cruft. C and C++ should be more suitable.

How about CobolGolf? (-:

In COBOL there is plenty of room for maddening obfuscation and counterdocumentation, but syntax is deliberately boring and simple.

For the golf player, PythonLanguage and RubyLanguage come close to PerlLanguage, with most of the complexity but less options and more keywords.

FunWithPerl list members occasionally play PerlGolf on http://perlgolf.sourceforge.net/

See also ShortestWikiContest

or the top of ThreeLineSystem.


PerlGolf doesn't just turn code to LineNoise - surprisingly, it often adds clarity by reducing a problem to its essence.

If I can't refactor a problem's solution into one line of Perl, it's usually because there are really two problems that require separate solutions. That may sound exaggerated, but the more Perl I learn the truer I find it. --IlmariKaronen

That is a matter of chance. Often it is true that you make code shorter by removing the unnecessary bits. But sometimes you get shorter through a trick that obscures what is happening. For instance:

  $a=1;print$a-=$b+=$a*=-1,$/for 0..31

-- BenTilly


This is like the "one-liner guessing game" in APL. Player A writes a one-line program, shows it to B, and says: "Bet you can't guess what this does."

Perl Golf takes the opposite approach. Start with readable code and take turns compressing it. You can follow the whole lineage. It's like gradually whittling down prose, finding one word to replace several.


A favorite annual contest in "Quote Quad" (publication of the ACM SIGAPL) was to find 100 different APL expressions (all one liners, of course) to print the new year's digits using only those digits as literals. The entry with the fewest total characters won. (I wonder if the contest was run for 2000!)


This is exactly my problem with Perl. People want to stuff a whole program in a line. It's impossible to understand and cryptic.

Your problem is with Perl, or with people using Perl? Last I checked, nothing in Perl forced one to write cryptic one-liners. Indeed, the Perl programmer's motto is There's More Than One Way To Do It. Some are unreadable and cryptic; some aren't. It ultimately falls on the programmers' maturity and familiarity with Perl. -JohnPassaniti

Some of the best applications for Perl are one liners, although they'd be more in other languages. Just the other day a friend wanted to replace newlines with comma and space, and this is particularly neat in Perl:

  perl -pe 's/\n/, /'

Hah! That can be done easier using sed, but only if you have GNU sed:

  sed s/\n/, /

...or...

  sed -e "s/\n/, /" 

...which is essentialy the same as the perl version. The -e allows for multiple commands. Both forms do the same; -e is more explicit.

Except that that doesn't work in sed (at any rate, not GNU sed v3.02), because sed splits its input at newlines, which makes it hard to replace an actual newline. According to http://sources.redhat.com/ml/bug-gnu-utils/2000-09/msg00013.html , it is possible but "You have to use the relatively obscure feature of a 'hold buffer' to join lines, and a 'rerun' of part of the sed script ot split lines. In a nutshell: this kind of task is usually better done in awk or perl."

I'd be interested to see a sed one-liner which did accomplish this, but not sufficiently interested to try writing one myself :-).

According to a message in the same thread (but untried), replacing newlines with 'zzz':
  sed ':a;${s/\n/zzz/g;q;};N;ba'

In sed:

  :a
      N
      s/\n/, /
      ta

which can be written on one line as

  sed -e :a -e 'N; s/\n/, /; ta'

and finally using awk

  awk '{ printf $0 ", " }'

Witness PythonGolf? in progress at IckyWiki.
PerlGolf is currently played at: http://terje.perlgolf.org/ or http://terje2.perlgolf.org/ and also at: http://perlgolf.sourceforge.net/ see also: http://archive.develooper.com/golf@perl.org/

GregAllen

There is also a Polish PerlGolf site at: http://kernelpanic.pl/perlgolf-list.mx

There is also a PerlGolf IRC group at irc.stealth.net #perlgolf

There is some Perl golf in SphereOnlineJudge, where Perl beats out other languages by a slim margin. http://www.spoj.pl/problems/KAMIL/ http://www.spoj.pl/problems/SIZECON/ - Because of a 'glitch' in the rules, the competition has little to do with addition.
See also: RulesOfGeelf, ChaitinElegance.

I have understood Perl concepts better writing short programs than I have when I started writing C-like Perl. Well, *ahem*, the very first program I wrote in Perl was a reimplementation of the split function.
CategoryPerl, CategoryGolf?

EditText of this page (last edited January 24, 2007) or FindPage with title or text search