While working on how to make a single user MUD-type text adventure, I decided to test real quick how to make it into a module. I had made some minor modules before, so I basically knew what to do. Before continuing, here is the code:

#!/usr/bin/perl use strict; use warnings; package module; my $state = 1; my @exits = ("Major Motoko Kusanagi wishes you well!","Come back soon, + ya hear?","Bah, coward!","Go with God.","Stellaaaaaa!","Namo Kuan Yi +n."); sub welcome { print "Welcome to my game! Do you want to move:\n\n"; print "North?\n"; print "South?\n"; print "East?\n"; print "West?\n\n"; } sub movement { while ($state == 1) { welcome(); print "<120/120hps 50/50mana 100/100mvs> "; chomp(my $move = <STDIN>); if ($move =~ /^(north|south|east|west)$/i) { print "\nYou have moved $move.\n\n"; } elsif ($move =~ /^quit$/i) { print "\n$exits[rand @exits]\n\n"; $state = 0; } else { print "\nThat is not a valid option.\n\n"; } } } movement(); 1;

Okay, so I do everything I'm supposed to do, right? When I finally make a test script for it, and run it through perl -c, it actually executes the program, in addition to returning whether or not the syntax is ok.

After a bit of trial and testing of other things, looking through older modules, even looked in warning.pm, I finally figured out what the problem was:

I had left the invocation from the original script in, when I modified it to be a module! I took out the invocation movement(); and all was well.

So the lesson there is, sometimes the simplest things can trip you up, if you forget about them.

EDIT: Code updated, per ysth's recommendations.

  • Comment on Sometimes, the simplest things will trip you up, if you forget about them
  • Download Code

Replies are listed 'Best First'.
Re: Sometimes, the simplest things will trip you up, if you forget about them
by merlyn (Sage) on Mar 21, 2005 at 05:16 UTC
Re: Sometimes, the simplest things will trip you up, if you forget about them
by ysth (Canon) on Mar 21, 2005 at 06:55 UTC
    if ($move =~ /\b(north|south|east|west)\b/i) {
    I would strongly advise you to anchor that (and the quit check, too) with ^ and $ instead of \b's:
    $ perl -Mmodule Welcome to my game! Do you want to move: North? South? East? West? <120/120hps 50/50mana 100/100mvs> I am but mad north-northwest You have moved I am but mad north-northwest. Welcome to my game! Do you want to move: North? South? East? West? <120/120hps 50/50mana 100/100mvs> When the wind is southerly, I can te +ll a hawk from a handsaw That is not a valid option. Welcome to my game! Do you want to move: ...
    or be consistent and let my second try work since the first one did.
Re: Sometimes, the simplest things will trip you up, if you forget about them
by Anonymous Monk on Mar 21, 2005 at 10:16 UTC
    single user MUD-type text adventure

    Considering that MUD stands for Multi User Dungeon, what exactly is a "single user MUD-type text adventure"? How is it different from a "single user text adventure", or just a "text adventure"?

      I meant that it's supposed to look MUD-ish. Perhaps I could have worded it better.
        That's a bit like saying a real airplane is supposed to look like a model airplane. {grin}

        Really. Adventure-style games predated MUDs by about, oh, 20 years. Might as well describe it like it is, not like what it isn't.

        Kids today, oy!

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.