Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Adding a counter to a perl script

Posted on 2003-03-10
5
Medium Priority
?
218 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
3 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 85

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
Measuring Server's processing rate with a simple powershell command. The differences in processing rate also was recorded in different use-cases, when a server in free and busy states.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

581 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