bobcubsfan
Programmer
I have a file of zip codes. When the form contains a good zip code, my program finds it along with the city and state. If the zip is bad and is not found in the file, my program does not finish. It hangs the sever. I have tried eof as well as some other programming, but nothing seems to work.
Here is the code:
#!/usr/bin/perl
if ($ENV{'REQUEST_METHOD'} eq 'GET') {
@pairs = split(/&/, $ENV{'QUERY_STRING'});
} elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
} else {
print "Content-type: text/html\n\n";
print "<P>Use Post or Get";
}
foreach $pair (@pairs) {
($key, $value) = split (/=/, $pair);
$key =~ tr/+/ /;
$key =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~s/<!--(.|\n)*-->//g;
if ($formdata{$key}) {
$formdata{$key} .= ", $value";
} else {
$formdata{$key} = $value;
}
}
print "Content-type: text/html\n\n";
$my_name=$formdata{'Name'};
$my_address1=$formdata{'address1'};
$my_address2=$formdata{'address2'};
$my_phone=$formdata{'phone'};
$my_email=$formdata{'email'};
$my_zip=$formdata{'zip'};
$my_zip=substr($my_zip,0,5);
$found_flag = "F";
$my_state ="";
$my_city = "";
open (ZC, "ZIP_CODES.txt") ;
while ($found_flag = "F") {
$mystuff = <ZC>;
if ($mystuff eq "notfound") {
last;
}
if ($my_zip eq substr($mystuff,0,5)) {
$my_state = substr($mystuff,5,2);
$my_city = substr($mystuff,7,);
$found_flag = "T";
last;
}
}
close(ZC);
Here is the code:
#!/usr/bin/perl
if ($ENV{'REQUEST_METHOD'} eq 'GET') {
@pairs = split(/&/, $ENV{'QUERY_STRING'});
} elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
} else {
print "Content-type: text/html\n\n";
print "<P>Use Post or Get";
}
foreach $pair (@pairs) {
($key, $value) = split (/=/, $pair);
$key =~ tr/+/ /;
$key =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~s/<!--(.|\n)*-->//g;
if ($formdata{$key}) {
$formdata{$key} .= ", $value";
} else {
$formdata{$key} = $value;
}
}
print "Content-type: text/html\n\n";
$my_name=$formdata{'Name'};
$my_address1=$formdata{'address1'};
$my_address2=$formdata{'address2'};
$my_phone=$formdata{'phone'};
$my_email=$formdata{'email'};
$my_zip=$formdata{'zip'};
$my_zip=substr($my_zip,0,5);
$found_flag = "F";
$my_state ="";
$my_city = "";
open (ZC, "ZIP_CODES.txt") ;
while ($found_flag = "F") {
$mystuff = <ZC>;
if ($mystuff eq "notfound") {
last;
}
if ($my_zip eq substr($mystuff,0,5)) {
$my_state = substr($mystuff,5,2);
$my_city = substr($mystuff,7,);
$found_flag = "T";
last;
}
}
close(ZC);