home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 25: Programming / pc_actual_25.iso / Javascript / InteractiveWebDesignJavascript / Scripting / cgi1 / perl / cgi-bin / myperls.stuff < prev    next >
Encoding:
Text File  |  2001-07-18  |  12.2 KB  |  457 lines

  1. #!C:/perl/bin/perl
  2.  
  3. # order.pl
  4. ######################################
  5. # Order Management System
  6. #
  7. # A simple on-line database to demonstrate
  8. # the capabilities of Perl, Win32 and the Web.
  9. # Database is a text file; limited insert, 
  10. # update and delete features are implemented.
  11. #
  12. # Created: 1/2/98
  13. # Author: Michael L. Curry 
  14. # http://www.cs.uoregon.edu/~currym
  15. ######################################
  16. # Copyright 1998, This script may be 
  17. # distributed and modified as needed. 
  18. # Please credit the author as appropriate.
  19. ######################################
  20.  
  21. require("cgi-lib.pl");
  22. &ReadParse(*form);
  23. $|=1;     # turn on error messages
  24. print "Content-type: text/html\n\n";
  25. $error1 = 'You must select an order before you can view details';
  26. $error2 = 'You must select an order before you can fill it';
  27.  
  28. ######################################
  29. # File references
  30. #
  31. $applet = "order.pl";
  32. $base_server_path = 'http://localhost/interactiveinternettech/perl/cgi-bin/';
  33. $base_file_path = 'c:/webshare/wwwroot/interactiveinternettech/perl/cgi-bin/';
  34. $db = $base_file_path . "order.db";
  35.  
  36. #############################
  37. # Test all valid button selections
  38. #
  39. if ($form{'new.x'}) { &new; } # main page, new record button
  40.  
  41. elsif ($form{'view.x'}) # main page, view an order button
  42.     if ($form{'D1'}) {&view;} # view the order if they selected one
  43.     else {&main($error1);}  # complain if they forgot to select one
  44. }
  45. elsif ($form{'modify.x'}) { &modify; } # detail page, modify order 
  46. elsif ($form{'save.x'}) { &save; } # detail page, save new order button
  47. elsif ($form{'fill.x'}) # main page, fill the order button
  48.     if ($form{'D1'}) {&fill;} # delete the order
  49.     else {&main($error2);}  # complain if they forgot to select one
  50. else {&main();} # otherwise, give them the summary
  51.  
  52. ##################
  53. # Main: display main menu
  54. #
  55. sub main {
  56.     local ($msg) = @_;
  57.     &header('Main Menu', $msg);
  58.     &summary;
  59. }
  60. ##################
  61. # New 
  62. #
  63. sub new {
  64.     &header('Create a New Order');
  65.     &addFields('Place Order','save');
  66. }
  67. ##################
  68. # View 
  69. #
  70. sub view {
  71.     &header('View & Modify an Order');
  72.     &addFields('Update Order','modify');
  73. }
  74. ##################
  75. # Save
  76. #
  77. sub save {
  78.     &insert;
  79.     &header('Main Menu', 'Previous order inserted');
  80.     &summary;
  81. }
  82. ##################
  83. # Modify
  84. #
  85. sub modify {
  86.     &update_modify;
  87.     &header('Main Menu', 'Previous order updated');
  88.     &summary;
  89. }
  90. ##################
  91. # Fill
  92. #
  93. sub fill {
  94.     &update_delete;
  95.     &header('Main Menu','Previous order filled');
  96.     &summary;
  97. }
  98. ##################
  99. # Order Summary
  100. #
  101. sub summary {
  102.  
  103.     local $action = $base_server_path . $applet;
  104.     open (DAT, "< $db") || die "Can't open $db:$!<br>";
  105.  
  106. print <<SUMMARY1;
  107. <form action="$action" method="GET">
  108.     <input type="hidden" name="page" value="main order"><div
  109.     align="center"><center><table border="0">
  110.         <tr>
  111.             <td><input type="image" name="new"
  112.             src="../dkBluBtn.gif" align="bottom" border="0"
  113.             width="32" height="25"></td>
  114.             <td><font size="5">New Order </font></td>
  115.             <td> </td>
  116.             <td><input type="image" name="view"
  117.             src="../dkBluBtn.gif" align="bottom" border="0"
  118.             width="32" height="25"></td>
  119.             <td><font size="5">View Order</font></td>
  120.             <td> </td>
  121.             <td><input type="image" name="fill"
  122.             src="../dkBluBtn.gif" align="bottom" border="0"
  123.             width="32" height="25"></td>
  124.             <td><font size="5">Fill Order</font></td>
  125.         </tr>
  126.     </table>
  127.     </center></div><p align="center"><select name="D1" size="5">
  128. SUMMARY1
  129.  
  130.     while (<DAT>)
  131.     {
  132.     print "<option>$_</option>";
  133.     }
  134.  
  135. print <<SUMMARY2;
  136.     </select></p>
  137. </form>
  138. <div align="center"><center>
  139.  
  140. <table border="0">
  141.     <tr>
  142.         <td valign="top"><font size="5"><img src="../Fork_LiftF1D1.gif"
  143.         width="27" height="21"></font></td>
  144.         <td><font size="4">The Order Management System provides
  145.         current order status reflected in the local order
  146.         administration database. For configuration information, </font><a
  147.         href="../configur.htm"><font size="4">consult this document</font></a><font
  148.         size="4">.</font><p><font size="5"><strong>Instructions: </strong></font></p>
  149.         </td>
  150.     </tr>
  151.     <tr>
  152.         <td> </td>
  153.         <td><ol>
  154.             <li>Select New Order to create an order. </li>
  155.             <li>Select View Order and make changes to the order
  156.                 and view details.</li>
  157.             <li>Select Fill Order to delete the order.</li>
  158.         </ol>
  159.         </td>
  160.     </tr>
  161. </table>
  162. </center></div>
  163. <p><em>Developed by:</em> <a
  164. href="http://www.cs.uoregon.edu/~currym">Michael L. Curry</a> <br>
  165. ⌐ Copyright 1998, <a href="../configur.htm#About the OMS">See
  166. distribution notice</a></p>
  167. </body>
  168. </html>
  169.  
  170. SUMMARY2
  171.     close (DAT);
  172. }
  173. ##################
  174. # Add Fields
  175. #
  176. sub addFields {
  177. local ($label,$button) = @_;
  178.  
  179. if ($form{'D1'} && $form{'view.x'}) 
  180. {
  181.     ($id, $date, $part, $desc, $price, $qty, $status) = split(/::/, $form{'D1'});
  182.  
  183. }
  184. else {$id = &getindex;}
  185.  
  186. local $action = $base_server_path . $applet;
  187.  
  188. print <<FIELDS;
  189. <form action="$action" method="GET">
  190.     <div align="center"><center><table border="0">
  191.         <tr>
  192.             <td><font size="4">Order Id</font></td>
  193.             <td><font size="4"><input type="text" size="10"
  194.             name="T1" value="$id"></font></td>
  195.             <td><font size="4">Date Created</font></td>
  196.             <td><font size="4"><input type="text" size="10"
  197.             name="T2" value="$date"></font></td>
  198.             <td> </td>
  199.             <td> </td>
  200.         </tr>
  201.         <tr>
  202.             <td><font size="4">ISBN </font></td>
  203.             <td><font size="4"><input type="text" size="10"
  204.             name="T3" value="$part"></font></td>
  205.             <td><font size="4">Title</font></td>
  206.             <td colspan="3"><font size="4"><input type="text"
  207.             size="20" name="T4" value="$desc"></font></td>
  208.             <td> </td>
  209.             <td> </td>
  210.         </tr>
  211.         <tr>
  212.             <td><font size="4">Unit Price</font></td>
  213.             <td><font size="4"><input type="text" size="10"
  214.             name="T5" value="$price"></font></td>
  215.             <td><font size="4">Quantity</font></td>
  216.             <td><font size="4"><input type="text" size="5"
  217.             name="T6" value="$qty"></font></td>
  218.             <td> </td>
  219.             <td> </td>
  220.         </tr>
  221.         <tr>
  222.             <td><font size="4">Status</font></td>
  223.             <td colspan="3"><font size="4"><input type="text"
  224.             size="30" name="T7" value="$status"></font></td>
  225.             <td> </td>
  226.             <td> </td>
  227.             <td> </td>
  228.             <td> </td>
  229.         </tr>
  230.         <tr>
  231.             <td><p align="right"><input type="image" name="$button"
  232.             value="save new" src="../dkBluBtn.gif" align="bottom" border="0"
  233.             width="32" height="25"></p>
  234.             </td>
  235.             <td valign="top"><font size="4"><strong>$label</strong></font></td>
  236.             <td> </td>
  237.             <td> </td>
  238.             <td> </td>
  239.             <td> </td>
  240.             <td> </td>
  241.             <td> </td>
  242.         </tr>
  243.     </table>
  244.     </center></div><p><font size="4"> </font></p>
  245. </form>
  246. </body>
  247. </html>
  248.  
  249. FIELDS
  250. }
  251. ##################
  252. # Header
  253. #
  254. sub header {
  255.     local ($msg1, $msg2) = @_;
  256.  
  257. print <<HEADER;
  258. <html>
  259.  
  260. <head>
  261. <title>New Order</title>
  262. </head>
  263.  
  264. <body background="../Lava.gif" bgcolor="#FFFFFF" text="#000000"
  265. bgproperties="fixed">
  266.  
  267. <table border="0" width="100%">
  268.     <tr>
  269.         <td rowspan="2"><img src="../library.gif" width="121"
  270.         height="187"></td>
  271.         <td rowspan="2"><p align="center"><font color="#008080"
  272.         size="6"><strong>Electronic Library Document Order Management System</strong></font></p>
  273.         </td>
  274.     </tr>
  275. </table>
  276.  
  277. <center><p><font color="#800040" size="4"><strong>$msg2</strong></font></p>
  278. HEADER
  279. }
  280. ##################
  281. # Insert record into Database 
  282. #
  283. sub insert {
  284.  
  285.     local ($line);
  286.     $line = $form{'T1'}.'::'.$form{'T2'}.'::'.$form{'T3'}.'::'.$form{'T4'}.'::'.$form{'T5'}.'::'.$form{'T6'}.'::'.$form{'T7'}."\n";
  287.     open (DAT, ">> $db") || die "Can't open $db:$!<br>";
  288.     print DAT $line;
  289.     close(DAT);
  290. }
  291.  
  292. ##################
  293. # Update Database by modifying record
  294. #
  295. sub update_modify {
  296.  
  297.     local ($tempDB, $line, $pat); # vars
  298.     $tempDB = $db; 
  299.     $tempDB =~ s/\.db/\.tmp/;
  300.     #
  301.     # build the new line
  302.     $pat = $form{'T1'};
  303.     $line = $form{'T1'}.'::'.$form{'T2'}.'::'.$form{'T3'}.'::'.$form{'T4'}.'::'.$form{'T5'}.'::'.$form{'T6'}.'::'.$form{'T7'}."\n";
  304.     
  305.     #print "Pat: $pat <br>";
  306.     #
  307.     # write new line & other lines to temp db
  308.     #
  309.     open (DAT, "< $db") || die "Can't open $db:$!<br>"; #read from
  310.     open (TMP, "> $tempDB") || die "Can't open $tempDB:$!<br>"; #write to
  311.     while (<DAT>)
  312.     {
  313.     if ($_ =~ /^\s*$pat\:\:/)     
  314.     { print TMP $line; }
  315.     else { print TMP $_; } 
  316.     }
  317.     close (DAT); close (TMP);
  318.     #
  319.     # Now over write the db with the temp db
  320.     #
  321.     open (DAT, "> $db") || die "Can't open $db:$!<br>"; #write to
  322.     open (TMP, "< $tempDB") || die "Can't open $tempDB:$!<br>"; #read from
  323.     while (<TMP>) {print DAT $_;}
  324.     close (DAT); close (TMP);
  325. }
  326. ##################
  327. # Update Database by deleting record
  328. #
  329. sub update_delete {
  330.  
  331.     local ($tempDB, $line, $pat); 
  332.     $tempDB = $db;
  333.     $tempDB =~ s/\.db/\.tmp/;
  334.     $pat = $form{'D1'};
  335.  
  336.     # print "Pat: $pat <br>";
  337.     #
  338.     # eliminate line from temp db
  339.     #
  340.     open (DAT, "< $db") || die "Can't open $db:$!<br>"; #read from
  341.     open (TMP, "> $tempDB") || die "Can't open $tempDB:$!<br>"; #write to
  342.     while (<DAT>)
  343.     {
  344.     if ($_ =~ /$pat/) {
  345.         #print $_, "matched", $pat, "<br>";
  346.         next; # delete line
  347.     }
  348.     else {print TMP $_;}
  349.     }
  350.     close (DAT); close (TMP);
  351.     #
  352.     # Now overwrite the db with the temp db
  353.     #
  354.     open (DAT, "> $db") || die "Can't open $db:$!<br>"; #write to
  355.     open (TMP, "< $tempDB") || die "Can't open $tempDB:$!<br>"; #read from
  356.     while (<TMP>) {print DAT $_;}
  357.     close (DAT); close (TMP);
  358. }
  359. ##################
  360. # get index: returns new index number
  361. #
  362. sub getindex {
  363.     local (%hash, @tmp, $key, $lastID);
  364.     open (DAT, "< $db") || die "Can't open $db:$!<br>";
  365.     while (<DAT>) 
  366.     {
  367.     @tmp = split (/::/,$_);
  368.     $hash{$tmp[0]}=$tmp[3];
  369.     }
  370.     close (DAT);
  371.     foreach $key (sort(keys(%hash))) {$lastID = $key}
  372.     $lastID= $lastID + 1;
  373.     return ($lastID);
  374. }
  375. #!C:/perl/bin/perl
  376.  
  377. # strip.pl
  378. ######################################
  379. # Strip out un necessary text in 
  380. # periodical file & fake an ISBN #
  381. #
  382. $file = "periodicals.txt";
  383. $tempfile = "CARL-index.txt";
  384.  
  385. sub isbn 
  386. {
  387.     local ($num) = @_;
  388.  
  389.     while (length($num) < 10) {
  390.     $num += int(rand 9999999999);
  391.     }
  392.     return $num;
  393. }
  394.  
  395. open (DAT, "< $file") || die "Can't open $file:$!";
  396. open (TMP, "> $tempfile") || die "Can't open $tempfile:$!";
  397.  
  398. $flg = 0;
  399.  
  400. while (<DAT>) {
  401.  
  402.     if (($_ =~ /^\#/) || ($_ =~ /^--/) || ($_ =~ /^\s\s/)) 
  403.     {
  404.     print TMP $_;
  405.     next;
  406.     } 
  407.     if (($_ =~ /Our Price/) || ($_ =~ /Read more about this title/)) {next;}
  408.     if ($_ =~ s/(\d+)(\.)(\s*)([^\~]*)(\~)(.*)/$4/) 
  409.     {
  410.     $num = $1;
  411.     $isbn = &isbn($num);
  412.     $line = $_;
  413.     chomp($line);
  414.     $flg = 1; 
  415.     next;
  416.     }
  417.     if ($flg) {
  418.     if ($_ =~ /([^\/]*)(\/)([^\d]*)(\d*)/) {
  419.         $author = $1; 
  420.         $year = $4;
  421.         $flg = 0;
  422.         $line .= " Author: " . $author . " Published: " . $year;
  423.         $line .= " ISBN:" . $isbn;
  424.         print TMP "$line\n\n";
  425.     }
  426.  
  427.     }
  428. }
  429. close (TMP);
  430. close (DAT);
  431. print "done!\n";
  432. #!c:\perl\bin\perl
  433.  
  434. ######################
  435. # File: stuff.pl
  436. # Accepts a list of files as input arguments
  437. # Combines them into one big file
  438.  
  439. $flag = shift(@ARGV);
  440.  
  441. if ($flag ne '-o') {
  442.  
  443.     print "Usage: stuff.pl -o stuffedfile infile1 [infile2] ... [infileN]\n";
  444.     die;
  445. }
  446.  
  447. $outfile = shift(@ARGV);
  448. open (OUT, ">> $outfile") || die print "can't open $outfile: $!\n";
  449.  
  450. while (<ARGV>) { print OUT $_; }
  451.  
  452. close (OUT);
  453. print "done!\n";
  454.