http://www.perlmonks.org?node_id=695425


in reply to Re: General pattern for interactive text-mode script?
in thread General pattern for interactive text-mode script?

If you want to keep your sanity while programming,

Highly desirable. :)

you'll encode all of the nesting logic into a data structure,

Any chance I could get you to give a short example of such a data structure?

and all the code blocks marked with #... in your example into a dispatch table.

Hm. Haven't used dispatch tables before. Searching around a bit, it seems to be just a hash where the keys are "command" names and the values are references to functions. Will have a closer look at "When should I use a dispatch table?" and "Implementing Dispatch Tables", because I'm still not sure what it buys me over just calling the functions directly.

Then you write a small dispatching program that walks through the data structure, prints the prompt, analyses the results, makes a transition into the next state and calls your callback (maybe in a different order).

Oooh. Would love to see pseudocode if you were of a mind to take the time to write it out.

  • Comment on Re^2: General pattern for interactive text-mode script?

Replies are listed 'Best First'.
Re^3: General pattern for interactive text-mode script?
by moritz (Cardinal) on Jul 03, 2008 at 18:39 UTC
    Here is some stupid example code that doesn't do anything useful, but should give you an idea of how such a dispatch table and a state automaton might work:
    use strict; use warnings; my %states = ( main => { next => [ qw(intermediate end) ], on_enter => sub { print "You entered state main\n" }, }, intermediate => { next => [ qw(intermediate end) ], on_enter => sub { print "stupid side effect\n"; }, }, end => { next => [], on_enter => sub { print "Never called\n" }, }, ); my $current = $states{main}; while (@{$current->{next}}){ $current->{on_enter}->(); print "You can go to the following states from here:\n"; for (@{$current->{next}}){ print "\t$_\n"; } my $next = <STDIN>; chomp $next; if ($states{$next}){ # go to the next state: # TODO: check that this state transition is allowed $current = $states{$next}; } else { print "Sorry, don't understand your input, try again please\n" +; } }
Re^3: General pattern for interactive text-mode script?
by starbolin (Hermit) on Jul 03, 2008 at 23:13 UTC

    An ethereal Anonymous Monk writes:

    "...because I'm still not sure what it buys me over just calling the functions directly."
    Because you store references to the functions in the hash and can call the function indirectly by de-referencing the hash value instead of hard coding the subroutine name into a fixed control structure. Much easier, and safer, store a reference than to indirect reference via a string. It is very important for you future programing success that you learn to write and understand dispatch tables. You will encounter them in many programs and use them often in your own code.


    s//----->\t/;$~="JAPH";s//\r<$~~/;{s|~$~-|-~$~|||s |-$~~|$~~-|||s,<$~~,<~$~,,s,~$~>,$~~>,, $|=1,select$,,$,,$,,1e-1;print;redo}