Commit | Line | Data |
4536f655 |
1 | package World2; |
2 | |
3 | # This is class level cell with no constructor or alias registration. |
4 | # It has two command message handlers, one to get the name and one to set it. |
5 | |
6 | my $name = 'UNKNOWN' ; |
7 | |
8 | sub hello_cmd { |
9 | |
10 | return "Hello world from $name\n"; |
11 | } |
12 | |
13 | sub name_cmd { |
14 | |
15 | my ( $self, $msg ) = @_ ; |
16 | |
17 | my $data = $msg->data() ; |
18 | |
19 | return unless $data ; |
20 | |
21 | $name = ${$data} ; |
22 | |
23 | return ; |
24 | } |
25 | |
26 | |
27 | =head1 Stem Cookbook - World2 |
28 | |
29 | =head1 NAME |
30 | |
31 | World2 - A minimal class level B<Stem> cell with read/write data. |
32 | |
33 | =head1 DESCRIPTION |
34 | |
35 | This B<Stem> class level cell is an extension of the World1 class. It |
36 | still has a method named C<world_cmd> that will return the stored |
37 | name. The C<name_cmd> method takes a message and set the $name to its |
38 | data. |
39 | |
40 | =head2 COMMAND METHOD |
41 | |
42 | The following code snippet in the F<World2> class |
43 | cell is the method that will receive a hello command from a |
44 | remote sender. |
45 | |
46 | package World2; |
47 | |
48 | sub hello_cmd { |
49 | |
50 | return "Hello world!\n"; |
51 | } |
52 | |
53 | B<Stem> makes the creation of Command message handling methods very |
54 | I<easy>. Any return with defined data will automatically be sent back |
55 | to the sender of this command in a response type message. In the |
56 | method above we return the "Hello world!\n" string which will get printed on |
57 | the console. |
58 | |
59 | For more information on how a message is routed to its destination |
60 | cell in B<Stem> please see the F<Stem Messaging Design Notes>. |
61 | |
62 | =head1 THE CONFIGURATION FILE |
63 | |
64 | The following B<Stem> configuration file is used to bring a |
65 | World2 class level cell into existance in the B<Stem> environment. |
66 | |
67 | [ |
68 | class => 'Stem::Console', |
69 | ], |
70 | [ |
71 | class => 'World2', |
72 | ] |
73 | |
74 | The first entry is C<Stem::Console>, class level cell allows a user to |
75 | manually send command messages into the B<Stem> system. It is not |
76 | required for this module, but it is used in this example to send |
77 | messages to the World2 class and to print responses from it. The |
78 | second entry loads the C<World2> class. We can now refer to this class |
79 | cell as I<World2> when we want to send it a message. |
80 | |
81 | =head1 USAGE |
82 | |
83 | Execute C<run_stem world> from the command line to run this configuration. |
84 | You will be greeted with the B<StemE<gt>> prompt. It is now |
85 | possible to send a message manually to I<World2>. Type the following |
86 | command at the B<Stem> prompt: |
87 | |
88 | B<World2 hello> |
89 | |
90 | This is standard B<Stem> Console syntax, the cell address followed by |
91 | the command name. This will send a message world_cmd method in the |
92 | C<World2> class cell. That method returns a value, which is converted |
93 | into a response message addressed to Stem::Console (the originator of |
94 | the command message), and its data will be printed on the console terminal. |
95 | |
96 | B<"Hello world!"> |
97 | |
98 | =head1 SEE ALSO |
99 | |
100 | F<Stem Cookbook Part 2> |
101 | |
102 | F<Stem Cookbook Part 3> |
103 | |
104 | =cut |
105 | |
106 | 1; |