Gossamer Forum
Home : General : Perl Programming :

learning using perl. Need help please!!!

Quote Reply
learning using perl. Need help please!!!
Hi all,

I have a report list report.data in flat text file. I would like to scan and compare what i have with the report.data.
I made and html file
Code:

<html><head><title>Scanning and compare Report</title></head>
<body bgcolor=white>
<BR>
<BR><CENTER>
<form action="compare.cgi" method="POST">
Scan serial number:
<input type="text" name="name" size=30>
<input type="submit" value="Search">
</form>
</CENTER>

</body>
</html>

and here is my compare.cgi

Code:

#!/usr/local/perl
use Time::Local;
$today = timelocal(localtime);
$now = &unix_to_date($today - 3600);

sub unix_to_date {

my ($date) = $_[0]; # reads in variable passed to subroutine reates array of month names
my (@months) = qw!01 02 03 04 05 06 07 08 09 10 11 12!; #c

@time = (localtime($date))[3,4,5,0,0]; #creates array of date from unix time

$time[2] += 1900; # converts year to proper format
return "$months[$time[1]]/$time[0]/$time[2] ";

}

$datafile = "report.data";

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ s/~!/ ~!/g;
$FORM{$name} = $value;
}

$searchstr = $FORM{'name'};

open(INF,$datafile);
@mydata = <INF>;
close(INF);
print "Content-type:text/html\n\n";
print "<html><head><title>Check Report</title></head>\n";
print "<body><center><h3>Check Report</h3></center>\n";

@results = grep(/$searchstr/i,@mydata);
if ($#results >= 0) {
foreach $i (@results) {
chomp($i);
($sn , $Location, $starttime, $endtime, $item, $family, $status) = split(/|/,$i);

print "<center><h2><font color=red> $FORM{'name'} This serial is in Report List </font></h2></center>\n";

open(OUTF,">>found_it.txt") or dienice("Couldn't open found_it.txt for writing: $!");

# This locks the file so no other CGI can write to it at the
# same time...
flock(OUTF,2);
# Reset the file pointer to the end of the file, in case
# someone wrote to it while we waited for the lock...
seek(OUTF,0,2);

print OUTF "$FORM{'name'}\|$item\|\|$now\n";
}
} else {
print "<center><h2><font color=green> $FORM{'name'} . Alert! This serial number is not in Report list, Please check.</font></h2></center>\n";
open(OUTF,">>found_it.txt") or dienice("Couldn't open found_it.txt for writing: $!");

# This locks the file so no other CGI can write to it at the
# same time...
flock(OUTF,2);
# Reset the file pointer to the end of the file, in case
# someone wrote to it while we waited for the lock...
seek(OUTF,0,2);

print OUTF "$FORM{'name'}\|$item\|NA\|$now";
}
print "</body></html>\n";

Blush

Somehow it can not insert the $item in file record.data to file found_it.txt

Can you help me to rewrite this code.

Thank you for your help.

TD.
Quote Reply
Re: [britneythuyduong] learning using perl. Need help please!!! In reply to
Hi...first of all, I would get rid of;

Code:
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ s/~!/ ~!/g;
$FORM{$name} = $value;
}

and replace it with;

Code:
use CGI;
$INPUT = new CGI;

Then iunstead of referencing with $FORM{'var'}, use $INPUT->param('var'); Just makes things a little faster I believe, and also a little cleaner code Wink


Next thing I would do, is add some die statements for the 'opens', in case it doesn't work. Something like this at the bottom of the script;

Code:
sub error {

my $error = shift;
print $INPUT->header();
print "There was an error. It was: $error";
exit;
}

and replace the 'open' commands with something like this at the end of them (remove the ;)

|| die &error("$!");

e.g.
Code:
open(INF,$datafile) || die &error("$!");

If it still doesn't/won't work, then I would suggest placing print commands in parts of the script to debug it.,...see what gets called, the variables returned etc, and you should get there in the end. Its a game like this with Perl Wink

Hope that helps

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [britneythuyduong] learning using perl. Need help please!!! In reply to
Add -w to your perl path and also use strict; right at the top then run your script again and head for your error log :)