Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: General pattern for interactive text-mode script?

by moritz (Cardinal)
on Jul 03, 2008 at 17:35 UTC ( #695402=note: print w/ replies, xml ) Need Help??


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

If you want to keep your sanity while programming, you'll encode all of the nesting logic into a data structure, and all the code blocks marked with #... in your example into a dispatch table.

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).

Searching cpan for state might give some interesting results, chances are that somebody already implemented something similar.


Comment on Re: General pattern for interactive text-mode script?
Download Code
Re^2: General pattern for interactive text-mode script?
by Anonymous Monk on Jul 03, 2008 at 18:23 UTC
    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.

      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" +; } }

      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}

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://695402]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2014-08-30 09:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (292 votes), past polls