一直觉得perl写网页是一件很原始的事情,编写CGI-使用perl的书刚借来就弄丢了。后来帮群里的小朋友讨论到CGI,觉得有点意思,就尝试学了一下,编写了一个简单的,保存邮件列表,和现实邮件列表的就简单CGI,如下:
#!/opt/lampp/bin/perl
use CGI;
use Data::Dumper;
use Storable;
my @values=('CharityBase','Linux kernel mailing list','MailChimp','Melissa Data');
our $q = CGI->new();
my $name =$q->param("name");
my $mail=$q->param("mail");
my $list=$q->param("organnisation");
print $q->header;
print $q->start_html({-title=>"Join a Mainlist"} );
if($q->param("all") )
{
#display name list
my $allhash1 = retrieve('datafile');
print $q->h1( {-align => 'CENTER' },'This all mail list!');
foreach $tmp1 (@values)
{
print $q->h3("$tmp1");
my $firstlist= $allhash1->{$tmp1};
foreach (@$firstlist){
printf $q->ul( $q->li("%s (%s)") ), $_->{name},$_->{mail};
}
}
} else {
if ($q->param('submit') ){
if(-s 'datafile' == 0){
my $tmphash={};
foreach my $key (@values)
{
$tmphash->{$key}=[];
}
store $tmphash,'datafile';
}
my $allhash = retrieve('datafile');
my $element = {
name =>$name,
mail =>$mail,
};
push @{$allhash->{$list}}, $element;
store $allhash,'datafile';
}
print $q->h1( { -align => 'CENTER' }, "Sign Up Free!" );
print $q->start_multipart_form({-action=>"test.cgi",-method=>"POST"});
print $q->p('Please Enter your name', $q->textfield({-name=>"name",-value=>''}),);
print $q->p('Please Enter your mail', $q->textfield({-name=>"mail",-value=>''}),);
print $q->p('Please choose:',$q->popup_menu({-name=>'organnisation',-values=>\@values, }),);
print $q->p( $q->submit({-name=>"submit",-value=>'submit'}),
$q->reset({-name=>"reset",-value=>'clear'}),
$q->submit({-name=>"all",-value=>'show all list '}),
);
print $q->end_form();
}
print $q->end_html;