********** July 6th colorsv2.cgi (Version 2 of colors.cgi ********** #!/usr/bin/perl -w use CGI qw(:standard); # Written by Mark Jacobson on Thursday July 5th, 2001 # colors.cgi version 2 ---> colorsv2.cgi # Illustrates Subroutines (Chapter 13), aka Functions (Hour 8) # Array @c1 is used for the BGCOLOR value # Array @c2 is used to the TEXT color value @c1 = qw(Red Green Blue Black Purple Yellow White Pink Orange Brown); @c2 = qw(Blue Yellow Yellow White Yellow Purple Black Black Black Pink); ($bgColor, $textColor, $recentColors) = &getColors; print header, start_html(-title => "BGColor $bgColor, Text Color $textColor", -BGCOLOR => "$bgColor", -TEXT => "$textColor"), h1("This has background color $bgColor and text color $textColor"), start_form, "", submit('Random Color'), end_form; if ($recentColors =~ m/:/) { $recentCount = $recentColors =~ tr/://; print "\n


Previous $recentCount (BGCOLOR,TEXT) color pairs:" . "\n
    \n"; @c = split (/:/, $recentColors); for ($i = 1; $i <= $recentCount; $i++) { $colorCode = $c[$i]; print "
  1. ($c1[$colorCode],$c2[$colorCode])

    \n"; } print "\n

\n"; } print end_html; #################################################################### sub getColors { $REPEAT_LENGTH = 6; srand; @c1 = qw(Red Green Blue Black Purple Yellow White Pink Orange Brown); @c2 = qw(Blue Yellow Yellow White Yellow Purple Black Black Black Pink); if (param()) { $recently = param('recentcolors'); @c = split (/:/, $recently); do { $whichColor = int (rand(@c1)); $repeat = 0; for ($i=0; $i<@c; $i++) { if ($whichColor == $c[$i]) { $repeat = 1; } } } while ($repeat == 1); unshift (@c, $whichColor); if (@c > $REPEAT_LENGTH) { pop @c; } $recentColors = join(":", @c); } else { $whichColor = int (rand(@c1)); $recentColors = $whichColor; } return ( $c1[$whichColor], $c2[$whichColor], $recentColors ); }