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.


In reply to Sometimes, the simplest things will trip you up, if you forget about them by matra555

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.