Many are familer with CGI::Application and CGI::Prototype. Each of these can be seen as a way to define a set of states, with the user transitioning between them. However, they are both intimately tied to a web application framework. Though web applications is what I usually get paid to do, I've recently had the need for a more generic framework.

Thus, I'm developing a state transition module for user interfaces. In it, you define a module which handles that state and what other states the current state is allowed to jump to.

Here's a quick demonstration of an application that starts with a 'menu' state, and then allows the user to change the program's options, start a new file, or open an existing file. You can exit at any time from each state, except for Options.

use UI::State; my $state = UI::State->new; # Define the states we will use $state->add_states( qw/ menu new open options / ); $state->state( menu => { # Class that handles this state class => 'My::UI::Menu', # States allowed to jump to. The 'exit' # state is always implicitly available. next => [qw/ new open options exit /], }); $state->state( new => { class => 'My::UI::New', next => [qw/ menu exit /], }); $state->state( open => { class => 'My::UI::Open', next => [qw/ menu exit /], }); $state->state( options => { class => 'My::UI::Options', next => [qw/ menu /], }); # Error! State 'foo' was not declared in add_states() $state->state( foo => { class => 'My::UI::Foo', next => [qw/ menu exit /], }); $state->state( new => { class => 'My::UI::New', # Error! State 'bar' was not declared in add_states() next => [qw/ menu bar exit /], }); # Start running from 'menu' $state->run( 'menu' );

The run() method starts running a loop until one of the states returns exit. The exit state is always available, but can be overriden with your own cleanup code if needed--just create it with the state() method like any other state.

Each module is required to have a new() method which returns a blessed object, and a run() method which recieves the last state used. You perform whatever actions are needed within the state, and then return one of the states the module is allowed to return. Returning any other state is an error.

"There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.


In reply to Generic State Transition UI Library by hardburn

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.