http://www.perlmonks.org?node_id=23267
Category: Fun Stuff
Author/Contact Info Kris Koskelin - kris@angelanthony.com
Description: Just a small bit of code to "do the name game" on any name given. I tried to clean up my code quite a bit to appease the monks before posting it, but I'm sure someone's got some ideas on how to improve it. :-)
#!/usr/bin/perl

print "Content-type: text/html\n\n";
&showform();

if($ENV{'REQUEST_METHOD'} ne "GET"){
  my($myform, $game, $left);
  $myform=&translate_form();
  $$myform{'name'}=~s/(\w)(\w+)/\u$1\L$2/;# capitalize first letter, l
+c the rest
  my @letters=split(//, $$myform{'name'});
  while($letters[0]!~/^[aeiouy]/i){
   last if scalar(@letters)==0;
   shift @letters;
  }
  $game="!name! !name! Bo-B!left!\nBanana Fanna Fo-F!left!\nMe My Mo-M
+!left!\n!name!!\n";
  $$myform{'left'}=join("", @letters);
  $game=~s/!([^!]+)!/$$myform{$1}/sg;
  $game=~s/\n/<br>\n/g;

print <<END;
<HR>
<font face="Comic Sans MS, Verdana, Arial" size=4 color="#FF4444">
$game
</font>
END

}
 print <<END;
</body>
</html>
END


sub showform{
 print <<END;
<html>
<title>The Name Game</title>
<body>
<CENTER>
<form method="POST" action="namegame.cgi">
<input type="text" name="name" size=25>
<input type="submit" value="Name-Game it!">
</form>
</CENTER>
END

}

sub translate_form{
  my($myform, $pair, %myform);
  if ($ENV{'REQUEST_METHOD'} eq "GET"){ 
     $myform = $ENV{'QUERY_STRING'};
  }else{
     read(STDIN, $myform, $ENV{'CONTENT_LENGTH'});
  }  
  foreach $pair (split (/&/, $myform)) {
     $pair=~s/\+/ /g;
     my($name, $value) = split(/=/,$pair);  
     foreach($name, $value){
       s/%(..)/pack ("C", hex ($1))/eg; 
       s/^\s*//sg; # remove leading spaces
       s/\s*$//sg; # remove trailing spaces
     }
     $myform{$name} .= "\0" if (defined($myform{$name})); 
     $myform{$name} .= $value;
  } 
  return \%myform;
}
Replies are listed 'Best First'.
RE: The Name Game
by turnstep (Parson) on Jul 19, 2000 at 23:28 UTC

    Well, you did ask (sort of), so:

    $myform->{'name'}=~s/(\w)(\w+)/\u$1\L$2/; ## You can use perl's builtin functions to do the job: $myform->{'name'}=ucfirst(lc($myform->{'name'});

    You could also replace:

    my @letters=split(//, $myform->{'name'}); while($letters[0]!~/^[aeiouy]/i){ last if scalar(@letters)==0; shift @letters; } $myform->{'left'}=join("", @letters); ## with: ($myform->{'left'}=$myform->{'name'}) =~ s/$[^aeiouy]+//;
Re: The Name Game
by thabenksta (Pilgrim) on May 02, 2001 at 00:57 UTC

    Very nice, this will keep me entertained for hours.

    -thabenksta
    my $name = 'Ben Kittrell'; $name=~s/^(.+)\s(.).+$/\L$1$2/g; my $nick = 'tha' . $name . 'sta';