A robot with an associated grid. To create the robot, use

    my $robot = 'Karel::Robot'->new;
    my $grid  = 'Karel::Grid'->new(x => 10, y => 12);
    $robot = $robot->set_grid($grid, 1, 1);


$robot->x, $robot->y
    my ($x, $y) = map $robot->$_, qw( x y );

Coordinates of the robot in its grid.

    my $grid = $robot->grid;

The associated Karel::Grid object.

$robot->set_grid($grid, $x, $y, $direction);

Initialize the grid. Grid must be an object of the Karel::Grid type, $x and $y are coordinates of the robot, $direction is one of N E S W (defaults to N). Dies if the robot's place is occupied by a wall.


Drop mark in the current location. Dies if there are already 9 marks.


Picks up one mark from the current location. Dies if there's nothing to pick.

  my $direction = $robot->direction;

Returns the robot's direction: one of qw( N W S E ).


Turn the robot to the left.


Returns the robot's coordinates, i.e. x and y.


Returns the grid element at the robot's coordinates, i.e.


Returns the coordinates of the grid element the robot is facing.


Returns the contents of the grid element the robot is facing.


For debugging Karel programs: returns the source of the currently executed command, current position in the source and the length of the command.


Run the given command.


Moves the robot one cell forward in its direction.

$robot->repeat($count, $commands)

Runs the repeat command: decreases the counter, and if it's non-zero, pushes the body to the stack. Returns 0 (CONTINUE) when it should stay in the stack, 1 (FINISHED) otherwise.

$isnot_south = $robot->condition('!S')

Solve the given condition. Supported parameters are:

  • N E S W

    Facing North, East, South, West

  • m

    Covering mark(s).

  • w

    Facing a wall.

  • !

    Negates the condition.

Returns true or false, dies on invalid condition.

$robot->If($condition, $commands, $else)

If $condition is true, puts $commands to the stack, otherwise puts $else to the stack. Returns 2 (FINISH_DELAYED) in the former case, 1 (FINISHED) in the latter one.

$robot->While($condition, $commands)

Similar to If, but returns 0 (CONTINUE) if the condition is true, i.e. it stays in the stack.


Checks whether the robot knows the command, and if so, pushes its definition to the stack. Dies otherwise. Returns 2 (FINISH_DELAYED).


Stops execution of the current program and clears the stack. Returns -1 (QUIT).


Makes one step in the currently running program.


There are three special return values corresponding to the stack handling:


If a command returns CONTINUE, the stack doesn't change. If it returns FINISHED, the following command in the stack is executed. If it returns FINISHED_DELAYED, new commands are put in the stack, but once they're finished, the command behaves as if finished, too.