Link to home
Start Free TrialLog in
Avatar of bravo_zulu
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{name}</B><BR>\n";
     print "<FONT COLOR=#000080>Date Submitted:</FONT>  <B>$FORM{day} $FORM{month} $FORM{year}</B><BR><BR>\n";
     
     print "<FONT COLOR=#000080>Phone Number:</FONT>    <B>($FORM{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><BR>\n";
     print "<FONT COLOR=#000080>Does this Snag ground the A/C?</FONT>  <B>$FORM{grounded}</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.print()><img src=/images/printbutton.gif 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);

}

Avatar of archaic0
archaic0
Flag of United States of America image

You could use a txt file on the server to store a unique number.  I use this method in many programs to create a sessionid to tie events to:

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.
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bravo_zulu
bravo_zulu

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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B>$FORM{name}</B><BR>\n";
      print "<FONT COLOR=#000080>Date Submitted:</FONT>&nbsp;&nbsp;<B>$FORM{day} $FORM{month} $FORM{year}</B><BR><BR>\n";
      
      print "<FONT COLOR=#000080>Phone Number:</FONT>&nbsp;&nbsp;&nbsp;&nbsp;<B>($FORM{phone}) $FORM{phone1}-$FORM{phone2}</B><BR>\n";
      print "<FONT COLOR=#000080>E-mail Address:</FONT>&nbsp;&nbsp;<B><A HREF=MAILTO:$FORM{email}>$FORM{email}</A></B><BR><BR>\n";
      print "<FONT COLOR=#000080>Nature of Snag:</FONT>&nbsp;&nbsp;<B>$FORM{S1}</B><BR><BR>\n";
      print "<FONT COLOR=#000080>Does this Snag ground the A/C?</FONT>&nbsp;&nbsp;<B>$FORM{grounded}</B></P>\n";
      print "<P>&nbsp;</P>\n\n";

      print "<h3><B><font color=#800000>Print this page and leave in the</font><font color=#00660>&nbsp;A/C Journey Log</B></font></h3>\n";

      print "<A HREF=javascript:window.print()><img src=/images/printbutton.gif width=115 height=20 alt='Send to Printer' border=0></a>\n";

      print "<H2>Snag Disposition</H2>\n";
      print "<p>Corrected&nbsp;&nbsp; <input type=checkbox>&nbsp;&nbsp;Deferred&nbsp;<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>&nbsp;</p>\n";
      print "<hr><br><hr><br><hr><br><hr><br><hr><br><hr><br><hr><br><hr><br><hr><br><hr><br><p>&nbsp;</p>\n";
      print "<FONT COLOR=#000080>Signed By:</FONT>&nbsp;&nbsp;..........................................................................................&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<FONT COLOR=#000080>Dated:</FONT>&nbsp;&nbsp;...............................................................</P>\n";
      print "</BODY>\n";
      print "</HTML>\n";

      exit(0);

}