Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^3: General pattern for interactive text-mode script?

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


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

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


Comment on Re^3: General pattern for interactive text-mode script?
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (15)
As of 2015-07-01 19:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (17 votes), past polls