The Perl Advent Calendar needs more articles for 2022. Submit your idea today!


Games::PerlWar - A Perl variant of the classic Corewar game


        This is a sparring program, similar to the programmed reality of the Matrix. 
        It has the same basic rules, rules like gravity. What you must learn is that 
        these rules are no different than the rules of a computer system. Some of 
        them can be bent, others can be broken.  - Morpheus

PerlWar is inspired by the classic|Corewar game. In this game, players pit snippets of Perl code (called 'agents') against each other in order to gain control of the vicious virtual battlefield known as... the Array.


Size of the Array

The number of cells that the Array possesses. Each cell can hold one agent.

Agent Maximal Size

The maximal length, in characters, of an agent. If an agent is or becomes larger than this limit, it automatically segfaults upon execution.

Game Maximal Number of Iterations

The maximal number of rounds that can be played before a game is declared over.


A turn of the game is made up of the following steps:

Introduction of New Agents

Each player has the opportunity to submit a new agent to be introduced into the Array. Insertions are treated in order of submission time. The cell into which an entrant agent lands is picked randomly amongst the empty positions of the Array.

If there are no empty positions left, an entrant agent replaces one of the already-present agents owned by the player, chosen randomly. If the player doesn't have any agents already present in the Array, the new agent is discarded.

Elimination of Players

A player is eliminated from the game if he doesn't have any agents present in the Array at the end of the introduction of new agents.

Running the Array

Each cell of the Array is visited sequentially. If a cell contains an agent, it is executed (agents exceeding the permitted length segfault on initialization).

Variables accessibles to the agents

Variables which, if modified, affect the Array are marked by a (M).

(M) $_

the code of the agent. Changes brought to $_ are reflected in the Array (that is, once executed, the agent's code is updated to the new value of $_).


the whole Array, positioned relatively to the current agent. (i.e., $_[0] eq $_ )

(M) $o (small oh)

the agent's facade (that is, the player it pretends to own allegiance to). Modifications to this affect the Array.

@o (small oh)

Array holding the facade of all agents, positioned relatively to the current agent.

$O (big oh)

The agent's true master.

$S, $I, $i

the game's parameters $S (max agent's size) and $I (max # of iterations), plus the current iteration $i. Those are local variables and can't be used to modified the game parameters, obviously.


If the agent segfaults, it is erased and the cell ownership is cleared.

If the agent executes without segfault'ing, the changes made to $_ (that is, on the snippet itself) are brought to the Array. E.g., the snippet

        $turn = 13; s/\d+/$&+1/e;

once executed, will become

        $turn = 14; s/\d+/$&+1/e;

In addition, the agent can return an instruction (see below). If it returns a value that does not match any valid instruction, it is treated as a no-op.

Agent Return Instructions

An agent can return one instruction of the set below. All positions are are relative to the position of the executing agent.


Copy the code of cell $x of @_ (as defined after execution of the agent) into position $y of the Array (relative to the position of the current agent). $x and $y must be integers with an absolute value between 0 and $#_. If either $x or $y are not explicitly given, they default to the position 0. If the destination position is already occupied by an agent belonging to a different player, the copy fails. If the copy succeed, the newly copied agent will only become operational during the next iteration (i.e., it will not be executed during the current iteration).


        # crawler - copy itself to the next position
        return ":1"

Nuke the agent presents in position $x. The cell then returns to its empty and unowned state. If $x is not given, defaults to 0.


        # berserker
        return '!'.1+int(rand(@_))

Update the agent in position $x of the Array by its counterpart in @_. If $x is not explicitly given, defaults to 0. Ownership of the modified agent isn't modified. If an agent isn't present, nothing happens.


        # drive neighbor to suicide
        $_[1] =~ s//return "!"/;
        return "~1";

Claim ownership of the agent in position $x of the Array. If there is no agent at that position, nothing happens.


        # crawling borg
        $pos = 1; 
        return "^$pos";


The game ends once the final round is played, or until all but one player have been eliminated. The winner of the game is the player with the most agents still alive in the Array.


Mambo War

In this variant, the agent maximal size is decremented after each turn.


This document describes the rules of PerlWar as implemented in Games::PerlWar v0.02.