View on
MetaCPAN is shutting down
For details read Perl NOC. After June 25th this page will redirect to
Uri Guttman > Stem-0.11 > World2



Annotate this POD


New  5
Open  0
View/Report Bugs

Stem Cookbook - World3 ^


World2 - A simple object level Stem cell.


This cell is an extension of the World1 cell. In this example, instead of a single class cell with a fixed response value, we now can create multiple cells (registered objects) each with their own private data. The world_cmd method will return the planet's name stored in the cell.


This cell illustrates the basic way to construct objects in Stem.


Object cells require an attribute specification that describes the information we want to exist independently in each object cell when it is created. The following is the attribute specification used in World2:

$attr_spec = [ { 'name' => 'planet', 'default' => 'X',



This specification indicates that this cell has an attribute named planet. It will default to the value of X if this attribute is not specified in the configuration arguments for this cell. Some of the attribute specification tags are name, type, default, required, class, and help. For more information on cell configuration please see Stem Object and Cell Creation and Configuration Design Notes and Stem Cell Design Notes.


This is a minimal Stem constructor with the usual name new. you can invoke any other method as a constructor from a configuration by using the 'method' field:

sub new {

    my ( $class ) = shift;

    my $self = Stem::Class::parse_args( $attr_spec, @_ );
    return $self unless ref $self ;

    return ( $self );


To create a Stem object cell we call the Stem::Class::parse_args routine and pass it the object cell attribute specification and the rest of the arguments passed into this constructor. The rest of the arguments come from the args field in the configuration for this cell. The parse_args function then returns the newly created object to the caller, which is usually the configuration system but it could be any other code as well. An important observation to make here is the Stem error handling technique. Errors, in Stem, are propagated up the call stack bu returning an error string rather than a reference. This is the typical Stem way of determining whether of not an error condition had occurred. Constructors or subroutines which normally return objects or references will return a string value as an error message. This is always checked by the caller and will usually be passed up the call stack until a top level subroutine handles it.


The following Stem configuration file is used to bring a World2 object level cell into existance in the Stem environment.

[ class => 'Console', ],

[ class => 'World2', name => 'first_planet', args => [],


[ class => 'World2', name => 'second_planet', args => [ planet => 'venus',



As explained in, we create a Stem::Console cell to allow for the creation of a Stem console to manually send command messages and display their responses. We also create two object level World2 cells. The first, we name first_planet and it defaults to having its planet attribute set to 'first_planet'. The second, we name second_planet and set its planet attribute to 'venus'.

Using the args specifier in the cell configuration indicates that we are creating an object cell rather than a class cell. It indicates to the Stem cell creation environment that we wish to execute the constructor of the specified class to create an object of the class rather than using the Stem module as a class itself. Using object cells allow us to instantiate multiple objects with unique values, addressed and subsequent behavior.


Execute run_stem world2 from the command line to run this configuration. You will be greeted with the Stem> prompt. It is now possible to send a message manually into the system.

Type the following at the Stem prompt:

reg status

This will show the status of the local Stem hub. You will notice the two entries for the object cells created by the configuration file under the object cell section.

Now execute the same command as you did in World1:

first_planet hello

Hello, World! (from X)

second_planet hello

Hello, World! (from venus)

As in World1, the above triggers the hello_cmd method. However, now we are triggering the hello_cmd method on separate object cells rather than a single class cell.


Stem Cookbook Part 1

Stem Cookbook Part 3

World2 Module

syntax highlighting: