Lady_Aleena,
Here is some advice on your code. In addition, I recommend reading the advice I gave at Re: Refactoring a large script.
#!/usr/bin/perl use strict; use warnings;

This is a great start to every piece of perl code. I would recommend adding the -T flag for web applications. If you don't understand what that does, read perlrun.

use CGI; use CGI::Carp qw(fatalsToBrowser);

Using CGI.pm to generate HTML is typically considered inferior to using a templating module. One advantage is keeping your data separate from your logic. Another is ease of modification. Additionally, fatalsToBrowser should almost never be used in production code - it is a security risk.

print "content-type: text/html \n\n";

Why use the CGI.pm module if you aren't really using it.

my @Dice = ( roll '1d4', roll '1d6', roll '1d8', roll '1d10', roll '1d12', roll '1d20' ); my @Dice2 = qw(1d4 1d6 1d8 1d10 1d12 1d20);

You should take a look into perlstyle and Perl::Tidy. I would have written that as:

my @Dice = qw(1d4 1d6 1d8 1d10 1d12 1d20); my @Roll = map { roll($_) } @Dice;
my @Radius = ( "1' or Touch", "5'", "10'", "20'", "50'", "100'" ); my $radius = "in a $Radius[rand @Radius] radius";

You need to work on choosing your variable names. Later on, are you going to be able to tell the difference between $radius and $Radius?

my @Wizard_School = qw(Abjuration Air Alteration Conjuration/Summoning + Divination Earth Enchantment/Charm Fire Illusion/Phantasm Invocation +/Evocation Necromancy Water); my @Cleric_Sphere = qw(Air All Animal Astral Chaos Charm Combat Creati +on Divination Earth Fire Guardian Healing Law Necromatic Numbers Plan +t Protection Summoning Sun Thought Time Travelers War Wards Water Wea +ther); my @Spell_Freq = ( "common", "uncommon", "rare", "very rare" );

You have a lot of data that appears to be static. I would look into keeping this data in distinct files and then writing a very small wrapper to load the data dynamically at run time. For instance:

sub load_array_data { my ($file, $array) = @_; open(my $fh, '<', $file) or die "Unable to open '$file' for readin +g: $!"; while (<$fh>) { chomp; push @$array, $_; } } my @Wizard_School; load_array_data('wizard.data', \@Wizard_School);

This way you can re-use the sub to load data from a file into any array you want and you can keep your code clutter free. There are modules that do this sort of thing for you so there isn't necessarily a need to write it yourself.

my @Colors = ( "f00;\">red", "ff0;\">yellow", "0f0;\">green", "0ff;\">cyan", "00f;\">blue", "f0f;\">magenta", "fff;\">white", "000;\">black" );

When you are mapping one item to another, a better data structure to use is a hash. See the following example:

my %color = ( blue => 1, green => 2, yellow => 3 red => 4, orange => 5, purple => 6 ); print "The code for green is $color{green}\n";

I believe this is more than enough to chew on for now.

Cheers - L~R


In reply to Re^2: Creating a random generator by Limbic~Region
in thread Creating a random generator by Lady_Aleena

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.