Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Text Based Perl Game

by kcott (Archbishop)
on Dec 18, 2013 at 14:03 UTC ( [id://1067629]=note: print w/replies, xml ) Need Help??


in reply to Text Based Perl Game

G'day spoonman2525,

Welcome to the monastery.

Here's another suggestion. You have multiple strings (for printing) that are hundreds of characters in length, wrap many times, and have huge amount of formatting (newlines and tabs) embedded in each string. Without actually running the code, it would be (virtually) impossible to visualise the output format. In addition, I suspect this might become something of a maintenance nightmare: consider a situation where you need to insert a phrase and then need to reposition a dozen or more newline ("\n") characters from the point of insertion to the end of the string.

I think you'll find the use of heredocs would be much easier. You can use these directly in the code or define them separately elsewhere in the code (such that the code logic is not obscured by large amounts of text).

Here's an example using part of the code in sub endingA {...}:

#!/usr/bin/env perl use strict; use warnings; print '-' x 40, "\n"; # (part of) Current code in sub endingA {...} print "\n\nYou prepare the escape pods for launch. One for yourself\n +and one for Doctor Willis. You quickly run back to the utility room\n +and carry him into the command room. You lay him down\nin one of the +escape pods, ...\n"; print '-' x 40, "\n"; print '-' x 40, "\n"; # Easier to read code for sub endingA {...} print <<'EOT'; You prepare the escape pods for launch. One for yourselfand one for Do +ctor Willis. You quickly run back to the utility roomand carry him in +to the command room. You lay him down in one of the escape pods, ... EOT print '-' x 40, "\n"; # Predefined text for use in sub endingA {...} my $endingA_text = <<'EOT'; You prepare the escape pods for launch. One for yourself and one for Doctor Willis. You quickly run back to the utility room and carry him into the command room. You lay him down in one of the escape pods, ... EOT print '-' x 40, "\n"; print $endingA_text; print '-' x 40, "\n";

Output:

---------------------------------------- You prepare the escape pods for launch. One for yourself and one for Doctor Willis. You quickly run back to the utility room and carry him into the command room. You lay him down in one of the escape pods, ... ---------------------------------------- ---------------------------------------- You prepare the escape pods for launch. One for yourself and one for Doctor Willis. You quickly run back to the utility room and carry him into the command room. You lay him down in one of the escape pods, ... ---------------------------------------- ---------------------------------------- You prepare the escape pods for launch. One for yourself and one for Doctor Willis. You quickly run back to the utility room and carry him into the command room. You lay him down in one of the escape pods, ... ----------------------------------------

Another, possibly better, alternative would be to define all these large tracts of text in an external file.

-- Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1067629]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-20 03:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found