guiltshackles has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks. This is my issue. I have been reading Perl for the absolute beginner. However there a lot of commands I am doing that give me errors like clear the screen. Anyways ultimately this is what I want to do. Make a story, for example. paragraph. decision 1 decision 2 and depending on the users input it points to another paragraph. Is Perl is capable of this? and if so can some one point me in the right direction or tutorial. Many thanks.

Replies are listed 'Best First'.
Re: Making a story?
by planetscape (Chancellor) on May 20, 2010 at 08:07 UTC
Re: Making a story?
by SilasTheMonk (Chaplain) on May 19, 2010 at 18:23 UTC
    If I understand you correctly you want to generate a story based upon how a user answers various questions. So basically you need to understand input and output. Also if you want to do things like clearing the screen, I guess you are looking for Curses, though I did not think anyone did that anymore. Maybe you want to checkout the NetHack name space.
Re: Making a story?
by scorpio17 (Canon) on May 19, 2010 at 21:41 UTC
    I think you can do this using just about any Wiki engine. That way the web browser becomes your GUI. The "paragraph" is like page 1. The "decisions" become links to other pages, etc. Try searching CPAN for perl wiki engines, or else download/install something like mediawiki.
Re: Making a story?
by tospo (Hermit) on May 20, 2010 at 12:49 UTC

    Of course you can do this with Perl but you might be better off using existing software like ADRIFT. I used that one some time ago and it is very good for the sort of thing you want to do.

    If you want to do this specifically to learn how to program then you need to look up how Perl can take user input (e.g. here) and how conditionals work. Any introductory Perl book will show you these techniques. Check out this free online book: Beginning Perl on perl.org.

      Hey thanks guys, I think im going to use TADS to make my story. Looks a little complicated but I think I'll figure it out. I thought it would be easier with Perl. Because I don't want to make it too complicated as TADS would like to make it. How would I make Perl point to a document display and it, than give you a choice and than the user input would point to another text file to display its contents, rinse and repeat.
        There are a lot of ways to attack that problem -- the question is are you more interested in writing the program, or in writing the story? I ask because you will probably be making a pretty serious commitment to learning Perl and coding to get to the point where you're writing the story.

        That said, here's a skeleton for a program:

        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; }
        Notice that the %locations data structure defines the locations and their relationships; in a really flexible program, you'd define a file format and read this in from it.

        The print_location routine looks for a static description file corresponding to the name of the location and prints it, or a generic "nothing to see" message if there's no description file.

        In my test story I created foo.txtwhich contained

        It's beautiful beyond belief here at foo!
        and bar.txt which contained
        It's cold, dark, and scary here at bar. You wish you were back at foo.
        This gets you custom descriptions for those locations, and the generic one for baz.

        read_destination shows the list of possible places you can go, and prompts you to enter one, making you keep entering places until you pick a good one or say 'quit'.

        Obviously this only lets you walk around without doing anything. If you wanted to add that, you'd need to expand read_destination to understand verbs as well as locations; you'd need to add objects (as in 'things', not code entities) and what room they were in, or if they were being carried, and then you'd need to handle verbs (or at least 'use') for the objects ,,, and pretty soon you've rewritten TADS.

        I encourage you to keep playing with this anyway; it's a great way to learn more Perl. I've used a few advanced techniques in here - a hash of arrays, and a hash slice to set up the "where can you go next" test, and I've installed a CPAN module (IO::Prompt), because that was easier than writing it myself.

        This is a jumping-off point that may be useful to you. I can't give you much more advice other than to say this is a great way to learn Perl, because you have an itch you want to scratch. Also, if you ever turn this into a generic program for reading a story file and creating an adventure, you've got a great CPAN module on your hands.

        Good luck, and happy programming!