use strict; use warnings; use IO::Prompt; $|++; # Map: # foo <-> baz # | ^ # | | # | V # +----> bar # You can get from foo to bar, but you have to return via baz. # You can also go from foo to baz to bar. my %locations = ( foo => [ qw(bar baz) ], bar => [ qw(baz) ], baz => [ qw(foo bar) ], ); my $current_location = 'foo'; while(1) { print_location($current_location); $current_location = read_destination($current_location); if ($current_location eq 'quit') { print "Bye!\n"; exit; } } sub print_location { my ($where) = @_; if (-f "$where.txt") { if (open my $fh, "<", "$where.txt") { while(defined($_ = <$fh>)) { print; } close $fh; } } else { print "There's nothing interesting here.\n"; } } sub read_destination { my ($where) = shift; my %next; @next{ @{$locations{$where}} } = (); print "You can go to: ", (join ', ', sort keys %next),"\n"; my $next_place; while(! $next_place or !exists($locations{$next_place})) { prompt "Pick a place: "; $next_place = $_; last if $next_place eq 'quit'; } return $next_place; }