Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re: Text Based Perl Game

by Athanasius (Chancellor)
on Dec 18, 2013 at 03:45 UTC ( #1067584=note: print w/replies, xml ) Need Help??

in reply to Text Based Perl Game

Hello spoonman2525, and welcome to the Monastery!

A few further comments:

  1. Anonymous Monk stole my thunder on this one, but definitely consider giving each lexical variable the smallest possible scope. For example, instead of one $command declared at the head of the script (thereby making it effectively global, despite its being declared with my), declare it each time it’s needed:

    sub mainMenu { ... chomp (my $command = <STDIN>); ...
  2. Also consider using lexical filehandles: open (my $OUT, '+>', DATAFILEOUT).

  3. The pragma use constant QUIT => 5; is actually implemented as a function, which is why you are getting warnings like this:

    Constant subroutine main::QUIT redefined at C:/Perl/Strawberry/strawbe +rry-perl- line 140.

    In other words, anything declared with use constant is a global. It’s better to give each constant a unique name:

    use constant QUIT_COMPUTER_COMMAND => 5;
  4. Reserve die for error conditions. For normal termination, prefer exit.

  5. In Perl, it’s really not necessary to define constants for TRUE and FALSE, and it’s certainly not necessary to name them explicitly when testing. For example, in sub bedroomCommand, it would be simpler to write:

    if ($light) { ... } else { ... }
  6. Familiarise yourself with Perl’s handy statement modifiers (see perlsyn). Then, instead of this (from sub loadGame):

    while (<$IN>) { chomp ($loadArray[$counter] = $_); $counter++; }

    you can write a single line, like this:

    chomp($loadArray[$counter++] = $_) while <$IN>;

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1067584]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2018-04-25 17:37 GMT
Find Nodes?
    Voting Booth?