Spoilers...

a_life is a CGI implementation of John Conway's "Life".

If you've never heard of it, imagine an infinite 2D grid.  Each space on the
grid can contain a live cell.  The fate of a cell depends on how many
neighbours it has (including diagonal neighbours, so up to 8).  A live cell
with fewer than 2 neighbours dies from loneliness, which one with more than
3 dies from overcrowding.  If an empty space has exactly 3 neighbours, a new
cell is born.  These simple rules give rise to a surprisingly complex world.

One interesting starting configuration is the r-pentomino:

  **
 **
  *

though this implementation isn't exactly optimised for speed, so you need
patience to follow it all the way through.

A rectangular grid containing the space around the live cells is output as
an HTML form with checkboxes for the cells (live cells are checked).  The
checkbox names are <x-coord>_<y-coord>.

Now lets look at the code:

 #!/usr/bin/perl

If you don't follow this line without help, you should probably stop
reading.

 use CGI;$a=new CGI;print$a->header =>$a-> start_html(substr($
 0,-4),q
 <olly@muscat.com>

This creates a CGI object, then prints out the Content-Type: line and the
blurb to begin the HTML page.  The page title is taken from the last 4
characters of the script name (in $0) i.e. "life".  The page author is my
email address - the < and > around the address actually quote this string
(using q<>).

 ),(startform$a
 get,a_life
 )=>
 submit

Now we print the HTML to start the form.  The form's method is "get" and the
action is "a_life" - i.e. this script.  Then we print the form's submit
button (submit is a method of the CGI object $a, which is at the start of
the next line).

 $a;sub _{$_{$#=$_._.$b}++}sub b{((@_=sort{$a-$b}keys%b)[0]-2 ..
 pop

This declares two subroutines, "_" and "b":

"_" has 3 uses.  The first is to set $# to $_."_".$b ($# is a deprecated
feature controlling the format of numeric output, but this effect is never
used - the variable name is just a bit of misdirection).  The second is to
return that entry from the %_ hash, and the third is to increment that hash
entry.

"b" take the keys of %b, sorts them numerically and returns a list from
<min key of b>-2 to <max key of b> (extracted from @_ using pop).

 )}for(param$a){do{_$_--;_$b--;_$_++;_++$_;$b{++$b}=_$b++;$a{$_--}=_;_;_$_--}
 if ($b)=/_(.+)/
 }

This loops over the names of all the CGI parameters (which is a list of all
live cells).  The "if" at the end filters out any parameters whose names
don't contain a "_" (e.g. ".submit"), and puts the y-coordinate into $b.  $_
is used as is for the x-coordinate, and the first decrement loses the "_"
and everything after.

So we call the subroutine _ with $_ and $b set to the x and y coordinates of
the eight cells next to each live cell.  Note that any parameters passed to
_ are ignored, so we just get the side effects.

We use the hashes %a and %b to keep track of the minimum and maximum x and y
coordinates.  The keys of these hashes track which coordinate values are
used (the keys store one more than each value used).

    map{$_{$_}=$_{$_}-2?
    $_
  {
  $_}==3:param$a$_}%_;

This runs through the %_ hash, and changes each element from a count of
neighbours to a value indicating whether that cell is alive or dead next
generation.  If a cell has 2 neighbours, it will only be alive next
generation if it's alive now (this is the second branch of the ?: ).
Otherwise a cell will be alive next generation is it has 3 neighbours
(regardless of whether it us alive now).

  for$b(b){%b=%a;print$a->br;map{print q?<INPUT
  type=checkbox ?
  ,_&&CHECKED," NAME=$#>"
  }b}

The for loop calls subroutine b to convert %b into the range of y values which
covers all cells with potential to live this generation.  It then iterates $b
over these.

Inside the loop, %b is set to %a in readiness for iterating over the x values.
an HTML "<br>" tag is output to start a new row, and then the map iterates
over the x values.

q?? is used to quote the first string, then _&&CHECKED calls subroutine _,
which returns the value for the current cell from %_, and sets $# to the
cell name.  If the cell isn't going to be alive, then _&&CHECKED is undef or
0, which either outputs nothing, or a harmless 0.  If it is to live, then
_ returns a true value and _&&CHECKED is the string "CHECKED".  Finally the
name of the checkbox ($# as set by _) is output.

Footnote: This started out as an attempt at a "4 line signature" entry,
until I realised there wasn't such a category this year.  For this reason,
it tends towards compactness rather than out-and-out obfuscation.

Olly Betts <olly@muscat.com>
