?
Solved

Adding a counter to a perl script

Posted on 2003-03-10
5
Medium Priority
?
187 Views
Last Modified: 2013-12-25
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);

}

0
Comment
Question by:bravo_zulu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 

Expert Comment

by:archaic0
ID: 8105213
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.
0
 
LVL 84

Accepted Solution

by:
ozo earned 100 total points
ID: 8117007
Be careful with that method archaic0, without locking the file you run the possibility that two programs running that code at the same time might reset the $id to 0
0
 

Author Comment

by:bravo_zulu
ID: 8118731
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);

}

0

Featured Post

Not sure which OpenStack Certification to get?

So you’ve realized you might want to get certified in OpenStack, but you’re not sure what the benefits might be or even which one you should take. You know there are several certification courses you can choose from, but how do you know which one is right for you?

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question