#!/usr/bin/perl -Tw
  use strict;
  $ENV{PATH} = "/bin:/usr/bin:/usr/ucb";
  
  use CGI qw(:standard);
  print
    header,
    start_html("Subscribe to Perl Hackers Weekly"),
    h1("Subscribe to Perl Hackers Weekly");
 if (my $error = validate_form()) {
   show_form($error);
   print end_html;
 } else {
   show_thank_you();
   print end_html;
   mail_request();
 }
 
 sub show_form {
   my $error = shift;
   print
     hr,
     ($error ? p($error) : ()),
     start_form,
     table(map
           Tr(td($_->[0]), td(textfield($_->[1],"",0,60))),
           ["Name", "name"],
           ["Address1", "address1"],
           ["Address2", "address2"],
           ["City", "city"],
           ["State", "state"],
           ["Zip code", "zip"],
           ["Daytime phone number", "dayphone"],
           ["email address", "email"],
          ),
       submit,
       end_form,
       hr;
 }
 
 sub validate_form {
   return "Tell us about you..." unless param(); # show initial form
   return "Missing name" unless param("name");
   return "Missing address 1" unless param("address1");
   return "Missing city" unless param("city");
   return "Missing state" unless param("state");
   return "Missing email" unless param("email");
   ## verify valid email addr syntax
   require Email::Valid;
   return "Bad email address syntax"
     unless Email::Valid->address(param("email"));
   return;                       # undef says good
 }
 
 sub show_thank_you {
   print p("Thank you! You should receive an email confirmation shortly.");
 }
 
 sub mail_request {
   open SM, "|/usr/lib/sendmail -oi -t"
     or die "Cannot launch sendmail: $!";
   print SM <<END;
 To: merlyn\@stonehenge.con
 From: merlyn\@stonehenge.con
 Subject: //SUBSCRIBE//FORM//
 
 END
 
   $CGI::Q->save(\*SM);
   close SM;
   die "sendmail exited with $?" if $?;
 }