bravo_zulu
asked on
Adding a counter to a perl script
I have a perl script which is used to report problems encoutered on an airplane (know as snags). This script emails the information entered by the user from a form to the aircraft maintenance engineer. It then prints the report out. I need to provide a control number on this report. Each time a report is submitted & printed out the control number must increment by one. That number must be included in the e-mail and be printed on the hard copy report.
The cgi file presently being used is:
#!/usr/bin/perl
# That is the path to PERL just above It MUST be first in the script
# The following accepts the data from the form
if ($ENV{'REQUEST_METHOD'} eq 'POST') {
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;
$FORM{$name} = $value;
}
#Send the e-mail
#Information of person who posted the FORM
open (MESSAGE,"| /usr/lib/sendmail -t");
print MESSAGE "To:$FORM{submitaddress}\n ";
print MESSAGE "From: $FORM{name}\n";
print MESSAGE "Reply-To:$FORM{email}\n";
print MESSAGE "Subject: CMQ Snag Report\n\n";
# add a line here to print the control number
print MESSAGE "This Snag Report has been submitted from the On-line Snag Report Form\n\n";
print MESSAGE "Submitted by: $FORM{name}\n";
print MESSAGE "Phone No: ($FORM{phone}) $FORM{phone1}-$FORM{phone2 }\n";
print MESSAGE "Email: $FORM{email}\n\n";
print MESSAGE "Date Reported: $FORM{day} $FORM{month} $FORM{year}\n";
print MESSAGE "Nature of Snag:\n\n";
print MESSAGE "$FORM{S1}\n\n";
print MESSAGE "Should the A/C be grounded: $FORM{grounded}\n\n";
close (MESSAGE);
&thank_you;
}
sub thank_you {
print "Content-type: text/html\n\n";
print "<HTML>\n";
print "<TITLE>Snag Report Form</TITLE>\n";
print "<H1><font color=#800000>Snag Report</H1></FONT>\n";
#add a line here to print the control number
print "<P><FONT COLOR=#000080>Submitted by:</FONT> <B>$FORM{na me}</B><BR >\n";
print "<FONT COLOR=#000080>Date Submitted:</FONT> <B>$FOR M{day} $FORM{month} $FORM{year}</B><BR><BR>\n" ;
print "<FONT COLOR=#000080>Phone Number:</FONT> <B>($FOR M{phone}) $FORM{phone1}-$FORM{phone2 }</B><BR>\ n";
print "<FONT COLOR=#000080>E-mail Address:</FONT> <B><A HREF=MAILTO:$FORM{email}>$ FORM{email }</A></B>< BR><BR>\n" ;
print "<FONT COLOR=#000080>Nature of Snag:</FONT> <B>$FORM{S1} </B><BR><B R>\n";
print "<FONT COLOR=#000080>Does this Snag ground the A/C?</FONT> <B>$FORM{grou nded}</B>< /P>\n";
print "<P> </P>\n\n";
print "<h3><B><font color=#800000>Print this page and leave in the</font><font color=#00660> A/C Journey Log</B></font></h3>\n";
print "<A HREF=javascript:window.pri nt()><img src=/images/printbutton.gi f width=115 height=20 alt='Send to Printer' border=0></a>\n";
print "<H2>Snag Disposition</H2>\n";
print "<p>Corrected <input type=checkbox> Deferred < input type=checkbox></p>\n";
print "<P><FONT COLOR=#000080>Action Taken:</FONT>(if deferred enter record in <B>Deferred Defect Log</B>)</P>\n";
print "<P> </p>\n";
print "<hr><br><hr><br><hr><br>< hr><br><hr ><br><hr>< br><hr><br ><hr><br>< hr><br><hr ><br><p> < /p>\n";
print "<FONT COLOR=#000080>Signed By:</FONT> .............. .......... .......... .......... .......... .......... .......... .......... ...... <FONT COLOR=#000080>Dated:</FONT > ....... .......... .......... .......... .......... .......... ......</P> \n";
print "</BODY>\n";
print "</HTML>\n";
exit(0);
}
The cgi file presently being used is:
#!/usr/bin/perl
# That is the path to PERL just above It MUST be first in the script
# The following accepts the data from the form
if ($ENV{'REQUEST_METHOD'} eq 'POST') {
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]
$FORM{$name} = $value;
}
#Send the e-mail
#Information of person who posted the FORM
open (MESSAGE,"| /usr/lib/sendmail -t");
print MESSAGE "To:$FORM{submitaddress}\n
print MESSAGE "From: $FORM{name}\n";
print MESSAGE "Reply-To:$FORM{email}\n";
print MESSAGE "Subject: CMQ Snag Report\n\n";
# add a line here to print the control number
print MESSAGE "This Snag Report has been submitted from the On-line Snag Report Form\n\n";
print MESSAGE "Submitted by: $FORM{name}\n";
print MESSAGE "Phone No: ($FORM{phone}) $FORM{phone1}-$FORM{phone2
print MESSAGE "Email: $FORM{email}\n\n";
print MESSAGE "Date Reported: $FORM{day} $FORM{month} $FORM{year}\n";
print MESSAGE "Nature of Snag:\n\n";
print MESSAGE "$FORM{S1}\n\n";
print MESSAGE "Should the A/C be grounded: $FORM{grounded}\n\n";
close (MESSAGE);
&thank_you;
}
sub thank_you {
print "Content-type: text/html\n\n";
print "<HTML>\n";
print "<TITLE>Snag Report Form</TITLE>\n";
print "<H1><font color=#800000>Snag Report</H1></FONT>\n";
#add a line here to print the control number
print "<P><FONT COLOR=#000080>Submitted by:</FONT> <B>$FORM{na
print "<FONT COLOR=#000080>Date Submitted:</FONT> <B>$FOR
print "<FONT COLOR=#000080>Phone Number:</FONT> <B>($FOR
print "<FONT COLOR=#000080>E-mail Address:</FONT> <B><A HREF=MAILTO:$FORM{email}>$
print "<FONT COLOR=#000080>Nature of Snag:</FONT> <B>$FORM{S1}
print "<FONT COLOR=#000080>Does this Snag ground the A/C?</FONT> <B>$FORM{grou
print "<P> </P>\n\n";
print "<h3><B><font color=#800000>Print this page and leave in the</font><font color=#00660> A/C Journey Log</B></font></h3>\n";
print "<A HREF=javascript:window.pri
print "<H2>Snag Disposition</H2>\n";
print "<p>Corrected <input type=checkbox> Deferred <
print "<P><FONT COLOR=#000080>Action Taken:</FONT>(if deferred enter record in <B>Deferred Defect Log</B>)</P>\n";
print "<P> </p>\n";
print "<hr><br><hr><br><hr><br><
print "<FONT COLOR=#000080>Signed By:</FONT> ..............
print "</BODY>\n";
print "</HTML>\n";
exit(0);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for your answers. I added the code to my file and edited it so that the text file would be located in the same directory. No joy however.
Also, a good point regarding adding a flock.
Any suggestions on how to incorporate the file lock and what the other problem may be? Below is what it looked like after adding the new code.
#!/usr/bin/perl
#create a unique id for each snag report
sub set_sessionid
{
open(FILE, "$unique_id.txt")
|| &create_sessionid;
$id = <FILE>;
close(FILE);
$old_id = $id - 3;
$new_id = $id + 1;
$unique_id = $new_id;
open(OUTPUT, ">unique_id.txt");
print OUTPUT "$new_id";
close(FILE);
}
sub create_sessionid
{
open(OUTPUT, ">unique_id.txt");
print OUTPUT "0";
close(FILE);
open(FILE, "$unique_id.txt")
|| &fatal_error;
$id = <FILE>;
close(FILE);
$old_id = $id - 3;
$new_id = $id + 1;
$unique_id = $new_id;
}
#end of unique id code
if ($ENV{'REQUEST_METHOD'} eq 'POST') {
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;
$FORM{$name} = $value;
}
#Send the e-mail
#Information of person who posted the FORM
open (MESSAGE,"| /usr/lib/sendmail -t");
print MESSAGE "To:$FORM{submitaddress}\n ";
print MESSAGE "From: $FORM{name}\n";
print MESSAGE "Reply-To:$FORM{email}\n";
print MESSAGE "Subject: CMQ Snag Report\n\n";
print MESSAGE "This Snag Report has been submitted from the On-line Snag Report Form\n\n";
print MESSAGE "Submitted by: $FORM{name}\n";
print MESSAGE "Phone No: ($FORM{phone}) $FORM{phone1}-$FORM{phone2 }\n";
print MESSAGE "Email: $FORM{email}\n\n";
print MESSAGE "Date Reported: $FORM{day} $FORM{month} $FORM{year}\n";
print MESSAGE "Nature of Snag:\n\n";
print MESSAGE "$FORM{S1}\n\n";
print MESSAGE "Should the A/C be grounded: $FORM{grounded}\n\n";
close (MESSAGE);
&thank_you;
}
#response back to users browser
sub thank_you {
print "Content-type: text/html\n\n";
print "<HTML>\n";
print "<TITLE>Snag Report Form</TITLE>\n";
print "<H1><font color=#800000>Snag Report</H1></FONT>\n";
print "<P><FONT COLOR=#000080>Submitted by:</FONT> &nbs p; &n bsp;<B>$FO RM{name}</ B><BR>\n";
print "<FONT COLOR=#000080>Date Submitted:</FONT> &nb sp;<B>$FOR M{day} $FORM{month} $FORM{year}</B><BR><BR>\n" ;
print "<FONT COLOR=#000080>Phone Number:</FONT> &nbs p;<B>($FOR M{phone}) $FORM{phone1}-$FORM{phone2 }</B><BR>\ n";
print "<FONT COLOR=#000080>E-mail Address:</FONT>   ;<B><A HREF=MAILTO:$FORM{email}>$ FORM{email }</A></B>< BR><BR>\n" ;
print "<FONT COLOR=#000080>Nature of Snag:</FONT> <B >$FORM{S1} </B><BR><B R>\n";
print "<FONT COLOR=#000080>Does this Snag ground the A/C?</FONT> <B> $FORM{grou nded}</B>< /P>\n";
print "<P> </P>\n\n";
print "<h3><B><font color=#800000>Print this page and leave in the</font><font color=#00660> A/C Journey Log</B></font></h3>\n";
print "<A HREF=javascript:window.pri nt()><img src=/images/printbutton.gi f width=115 height=20 alt='Send to Printer' border=0></a>\n";
print "<H2>Snag Disposition</H2>\n";
print "<p>Corrected <input type=checkbox> Deferred&n bsp;<input type=checkbox></p>\n";
print "<P><FONT COLOR=#000080>Action Taken:</FONT>(if deferred enter record in <B>Deferred Defect Log</B>)</P>\n";
print "<P> </p>\n";
print "<hr><br><hr><br><hr><br>< hr><br><hr ><br><hr>< br><hr><br ><hr><br>< hr><br><hr ><br><p>&n bsp;</p>\n ";
print "<FONT COLOR=#000080>Signed By:</FONT> .... .......... .......... .......... .......... .......... .......... .......... .......... ......&nbs p; &n bsp; &nbs p; &n bsp;<FONT COLOR=#000080>Dated:</FONT > &nb sp;....... .......... .......... .......... .......... .......... ......</P> \n";
print "</BODY>\n";
print "</HTML>\n";
exit(0);
}
Also, a good point regarding adding a flock.
Any suggestions on how to incorporate the file lock and what the other problem may be? Below is what it looked like after adding the new code.
#!/usr/bin/perl
#create a unique id for each snag report
sub set_sessionid
{
open(FILE, "$unique_id.txt")
|| &create_sessionid;
$id = <FILE>;
close(FILE);
$old_id = $id - 3;
$new_id = $id + 1;
$unique_id = $new_id;
open(OUTPUT, ">unique_id.txt");
print OUTPUT "$new_id";
close(FILE);
}
sub create_sessionid
{
open(OUTPUT, ">unique_id.txt");
print OUTPUT "0";
close(FILE);
open(FILE, "$unique_id.txt")
|| &fatal_error;
$id = <FILE>;
close(FILE);
$old_id = $id - 3;
$new_id = $id + 1;
$unique_id = $new_id;
}
#end of unique id code
if ($ENV{'REQUEST_METHOD'} eq 'POST') {
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]
$FORM{$name} = $value;
}
#Send the e-mail
#Information of person who posted the FORM
open (MESSAGE,"| /usr/lib/sendmail -t");
print MESSAGE "To:$FORM{submitaddress}\n
print MESSAGE "From: $FORM{name}\n";
print MESSAGE "Reply-To:$FORM{email}\n";
print MESSAGE "Subject: CMQ Snag Report\n\n";
print MESSAGE "This Snag Report has been submitted from the On-line Snag Report Form\n\n";
print MESSAGE "Submitted by: $FORM{name}\n";
print MESSAGE "Phone No: ($FORM{phone}) $FORM{phone1}-$FORM{phone2
print MESSAGE "Email: $FORM{email}\n\n";
print MESSAGE "Date Reported: $FORM{day} $FORM{month} $FORM{year}\n";
print MESSAGE "Nature of Snag:\n\n";
print MESSAGE "$FORM{S1}\n\n";
print MESSAGE "Should the A/C be grounded: $FORM{grounded}\n\n";
close (MESSAGE);
&thank_you;
}
#response back to users browser
sub thank_you {
print "Content-type: text/html\n\n";
print "<HTML>\n";
print "<TITLE>Snag Report Form</TITLE>\n";
print "<H1><font color=#800000>Snag Report</H1></FONT>\n";
print "<P><FONT COLOR=#000080>Submitted by:</FONT> &nbs
print "<FONT COLOR=#000080>Date Submitted:</FONT> &nb
print "<FONT COLOR=#000080>Phone Number:</FONT>
print "<FONT COLOR=#000080>E-mail Address:</FONT>  
print "<FONT COLOR=#000080>Nature of Snag:</FONT> <B
print "<FONT COLOR=#000080>Does this Snag ground the A/C?</FONT> <B>
print "<P> </P>\n\n";
print "<h3><B><font color=#800000>Print this page and leave in the</font><font color=#00660> A/C Journey Log</B></font></h3>\n";
print "<A HREF=javascript:window.pri
print "<H2>Snag Disposition</H2>\n";
print "<p>Corrected <input type=checkbox>
print "<P><FONT COLOR=#000080>Action Taken:</FONT>(if deferred enter record in <B>Deferred Defect Log</B>)</P>\n";
print "<P> </p>\n";
print "<hr><br><hr><br><hr><br><
print "<FONT COLOR=#000080>Signed By:</FONT> ....
print "</BODY>\n";
print "</HTML>\n";
exit(0);
}
sub set_sessionid
{
open(FILE, "$id_dir/unique_id.txt")
|| &create_sessionid;
$id = <FILE>;
close(FILE);
$old_id = $id - 3;
$new_id = $id + 1;
$unique_id = $new_id;
open(OUTPUT, ">$id_dir/unique_id.txt");
print OUTPUT "$new_id";
close(FILE);
}
sub create_sessionid
{
open(OUTPUT, ">$id_dir/unique_id.txt");
print OUTPUT "0";
close(FILE);
open(FILE, "$id_dir/unique_id.txt")
|| &fatal_error;
$id = <FILE>;
close(FILE);
$old_id = $id - 3;
$new_id = $id + 1;
$unique_id = $new_id;
}
My example provides for the file not existing and creating it the first time provided you set the $id_dir to a valid location.
It also sets an $old_id that I use to delete old files with - but you probably don't need that part.