473,434 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,434 software developers and data experts.

New page in perl

Hi. I need to create 3 pages. Page 1 requests the number of elements the user wants to add and average. Page 2 asks the user for the numbers. Page 3 adds and averages the numbers and displays them. I can't create a separate page 3 to do this. Page 3 shows the contents of page 2 in addition to the contents of page 3. How do I get only the contents on page 3 to display separately? Everything works so I'm just asking how to do this. Any feedback would be appreciated. Thanks in advance. Here is my code:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. BEGIN
  4. {
  5.     open(STDERR, ">&STDOUT");
  6.     select(STDERR); $| = 1;
  7.     select(STDOUT); $| = 1;
  8.     print "Content-type: text/html\n\n";
  9. }
  10.  
  11. if($ENV{'QUERY_STRING'} eq "")
  12. {
  13.     &page1;
  14.     exit;
  15. }
  16.  
  17. read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
  18.  
  19. if($buffer eq "")
  20. {
  21.     $buffer = $ENV{'QUERY_STRING'};
  22. }
  23.  
  24. @pairs = split(/&/, $buffer);
  25. foreach $pair(@pairs)
  26. {
  27.         ($name, $value) = split(/=/, $pair);
  28.  
  29.     $value =~ tr/+/ /;
  30.         $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  31.         $name =~ tr/+/ /;
  32.         $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  33.  
  34.     if($FORM{$name} eq "")
  35.     {
  36.         $FORM{$name} = $value;
  37.     }
  38.     else
  39.     {
  40.         $FORM{$name} .= " " . $value;
  41.         #$FORM{$name} /= " " . $value;
  42.  
  43.     }
  44. }
  45.  
  46. $num = $FORM{"num"};
  47.  
  48. sub page1
  49. {
  50. print <<ENDHTML;
  51. <title>Assignment #4 (CS Credit)</title>
  52. <body text="white" bgcolor="blue">
  53. <font face="arial" size="4">
  54. <div align="center">
  55. <form method="get" action="/~westj2/cgi-bin/assignment4_CS_credit.cgi">
  56. <br>
  57.  
  58. Enter the number of elements to add and average:
  59. <input type="text" name="num" value="3"><br><br>
  60. <input type="submit" name="sub" value="Submit">
  61. <input type="reset" name="res" value="Reset">
  62.  
  63. ENDHTML
  64. }
  65.  
  66. print <<ENDHTML;
  67.  
  68. <title>Assignment #4 (CS Credit)</title>
  69. <body text="white" bgcolor="blue">
  70. <font face="arial" size="4">
  71. <div align="center">
  72. <form method="get" action="/~westj2/cgi-bin/assignment4_CS_credit.cgi">
  73.  
  74. Enter elements you want to add and average:<br><br>
  75.  
  76. ENDHTML
  77.  
  78. for($i = 1; $i <= $num; $i++)
  79. {
  80.     print "Element #$i: ";
  81.     print "<input type='text' name=$i value=$i><br>";
  82. }
  83.  
  84.  
  85. print <<ENDHTML;
  86.  
  87. <br><input type="submit" name="calc" value="Submit">
  88. <input type="reset" name="res" value="Reset">
  89. <input type="hidden" name="num" value=$FORM{"num"}>
  90.  
  91. ENDHTML
  92.  
  93. my @queue;
  94.  
  95. for($i = 1; $i <= $num; $i++)
  96. {
  97.     unshift(@queue, $FORM{"$i"});
  98. }
  99.  
  100. my @elements = @queue;
  101.  
  102. print "You entered: ";
  103.  
  104. for($i = 1; $i <= $num; $i++)
  105. {
  106.     print(pop(@queue));
  107.     print " ";
  108. }
  109.  
  110. my $sum;
  111.  
  112. for($i = 0; $i < $num; $i++)
  113. {
  114.     $sum = $sum + @elements[$i];
  115. }
  116.  
  117. print "<br>Sum: $sum";
  118.  
  119. my $avg = $sum / $num;
  120. print "<br>Average: $avg";
  121.  
Jun 3 '09 #1
15 3458
KevinADC
4,059 Expert 2GB
Without going into the whole use "strict" and "warnings" and the CGI module speech, and changing too much of the code you already have, here is one way how (I think) you can do what you want. Ask questions if necessary.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. BEGIN {
  4.  
  5.     open(STDERR, ">&STDOUT");
  6.     select(STDERR); $| = 1;
  7.     select(STDOUT); $| = 1;
  8.     print qq{Content-type: text/html\n\n
  9. <title>Assignment #4 (CS Credit)</title>
  10. <body text="white" bgcolor="blue">
  11. <font face="arial" size="4">
  12. <div align="center">
  13. };
  14. }
  15.  
  16. my $url = '/~westj2/cgi-bin/assignment4_CS_credit.cgi';
  17.  
  18. read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
  19. $buffer = $ENV{'QUERY_STRING'};
  20.  
  21. @pairs = split(/&/, $buffer);
  22. foreach $pair(@pairs){
  23.    ($name, $value) = split(/=/, $pair);
  24.    $value =~ tr/+/ /;
  25.    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  26.    $name =~ tr/+/ /;
  27.    $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  28.    if($FORM{$name} eq ""){
  29.       $FORM{$name} = $value;
  30.    }
  31.    else{
  32.       $FORM{$name} .= " " . $value;
  33.    }
  34. }
  35.  
  36. $num = $FORM{"num"};
  37. if($FORM{'page'} == 2) {
  38.    page2();
  39.    exit;
  40. }
  41. elsif($FORM{'page'} == 3) {
  42.    page3();
  43.    exit;
  44. }
  45. else {
  46.    page1();
  47.    exit;
  48. }
  49.  
  50. sub page1 {
  51.    print <<ENDHTML;
  52. <form method="get" action="$url">
  53. <input type="hidden" name="page" value=2>
  54. <br>
  55.  
  56. Enter the number of elements to add and average:
  57.  
  58. <input type="text" name="num" value="3"><br><br>
  59. <input type="submit" name="sub" value="Submit">
  60. <input type="reset" name="res" value="Reset">
  61. </form>
  62.  
  63. ENDHTML
  64. }
  65.  
  66. sub page2 {
  67.    print <<ENDHTML;
  68. <form method="get" action="$url">
  69. <input type="hidden" name="page" value=3>
  70.  
  71. Enter elements you want to add and average:<br><br>
  72.  
  73. ENDHTML
  74.  
  75. for($i = 1; $i <= $num; $i++)
  76. {
  77.     print "Element #$i: ";
  78.     print "<input type='text' name=$i value=$i><br>";
  79. }
  80.  
  81.  
  82. print <<ENDHTML;
  83.  
  84. <br><input type="submit" name="calc" value="Submit">
  85. <input type="reset" name="res" value="Reset">
  86. <input type="hidden" name="num" value=$FORM{"num"}>
  87. </form>
  88.  
  89. ENDHTML
  90. }
  91.  
  92. sub page3 { 
  93.    my @queue;
  94.  
  95.    for($i = 1; $i <= $num; $i++){
  96.       unshift(@queue, $FORM{"$i"});
  97.    }
  98.    my @elements = @queue;
  99.    print "You entered: ";
  100.    for($i = 1; $i <= $num; $i++){
  101.       print(pop(@queue));
  102.       print " ";
  103.    }
  104.    my $sum;
  105.    for($i = 0; $i < $num; $i++){
  106.       $sum = $sum + $elements[$i];
  107.    }
  108.    print "<br>Sum: $sum";
  109.    my $avg = $sum / $num;
  110.    print "<br>Average: $avg";
  111.    print qq{<br><br><form action="$url">
  112. <input type="submit" value="Try Again...">
  113. </form>
  114. }; 
  115. }    
  116.  
Jun 3 '09 #2
Nope, it didn't work. It's doing the same thing. I thought using hidden values may work, but I guess not. What about using path info? Any thoughts?

EDIT: Actually, now that I look at it more closely, the text that is supposed to be separate on page3 is already being shown on page2. When the submit button is clicked on page2, the math is performed and the text is updated to show the sum and average. I think this was happening before I edited it with your suggestions as well.
Jun 3 '09 #3
KevinADC
4,059 Expert 2GB
I tested your code and my code and they do not do the same thing. My code shows each page in succession, and only that page. Your code shows page one, then page two with page three output displayed in page two, then page three output with page two output also displayed.

I assumed that was what you wanted to do judging by your not very clear description of the behavior you were seeking.
Jun 3 '09 #4
I copied your code and pasted it in a new file then ran it, and it's doing the same thing as mine for me. If you go to this link, you can run all three programs I have and their code as well.

http://web.cs.sunyit.edu/~westj2/cgi-bin/

The programs are assignment4_CS_credit.cgi, assignment4_CS_credit2.cgi, and assignment4_CS_credit3.cgi.

Version 3 is the program with the code you suggested.
Jun 3 '09 #5
RonB
589 Expert Mod 512MB
The link for the action attribute is pointing to the wrong script, which is why you think Kevin's script is doing the same thing as yours.

I'm still trying to decide if I want/should post a properly written script, which I probably won't because this is clearly a homework assignment. If I do, it will be one that your instructor won't accept, because it will use some "advanced" items that would probably not have been covered in your class.


Ron
aka FishMonger
Jun 3 '09 #6
Doh! Thanks Ron and Kevin. It works now.
Jun 4 '09 #7
KevinADC
4,059 Expert 2GB
@RonB
Yikes... I think this guy is stalking me! ;)
Jun 4 '09 #8
RonB
589 Expert Mod 512MB
@KevinADC
What should I do when I catch you! :) :)
Jun 5 '09 #9
KevinADC
4,059 Expert 2GB
@RonB

hmm.... never thought about that. Buy me a Margarita? ;)
Jun 5 '09 #10
RonB
589 Expert Mod 512MB
You know that I know the perfect place.
http://www.rosysatthebeach.com

Now, watch me get dinged for the semi self promotion
Jun 5 '09 #11
KevinADC
4,059 Expert 2GB
hehehe.... maybe one of these days I'll take you up on that. BTW, welcome to bytes.com :)
Jun 6 '09 #12
RonB
589 Expert Mod 512MB
@KevinADC
I might even get behind the bar, which usually means I lower their profit margin, so beware. :)

@KevinADC
Thanks,
I'm starting to see the other sites we're on slow down (or I'm just getting bored) so I need to add a few more to my "resume".
Jun 6 '09 #13
KevinADC
4,059 Expert 2GB
This perl forum used to be hopping. Look at my post count in just a little over two years as a member here. Sure, some of it is not in the perl forum but the majority is. This forum went nearly dead about 6 to 7 months ago when some forum changes were made. Its getting a little activity now but not even close to what it used to be.
Jun 6 '09 #14
NeoPa
32,556 Expert Mod 16PB
It's great to see you popping back Kevin - if only now and then.

No promises, but we're hoping to have a change (or two) released shortly which may allow the site to grow again (to stop it's further decline at least). Watch this space (but keep the breathing going - I don't have any dates yet).
Jun 7 '09 #15
numberwhun
3,509 Expert Mod 2GB
@NeoPa
That is absolutely good to hear. Thanks for the info! :)
Jun 7 '09 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: william c | last post by:
I don't really know PHP that well. I'm fixing a Perl program that accesses a db after getting form input from a PHP page. If the server-side validation fails I'd like to reload the form with all...
9
by: Andrew DeFaria | last post by:
I have somebody or some people who are abusing a Perl based .cgi script. I have written a PHP page which reports this abuse as per $_SERVER. My intentions were to replace the .cgi script with this...
2
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # suppose you want to fetch a webpage. from urllib import urlopen print urlopen('http://xahlee.org/Periodic_dosage_dir/_p2/russell-lecture.html').read() #...
5
by: crunix | last post by:
Hello. I have developed a medical application with php 4 linked to IBM DB2 database. The database connection is right and i can access data with no problem...but sometimes when i reload the...
6
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of...
0
by: Thomas Blabb | last post by:
Sorry for this question but how do I write a simple "Hello world" HTML page from a perl script? In other words if I click in an already existing html page on a link like: .... <a...
4
by: benwylie | last post by:
I am running IIS 6.0 on Windows 2003. I would like to be able to run a perl script from a web page and include the output. I have tried doing it with an ssi: <form action='docsearch.shtml'...
0
by: sandy | last post by:
hello,,,,,,,,, i am creating login page using Perl/CGI facing prob... able to connect DB but from there facing prob If u have related code of login page in Perl please send me on...
22
by: owlice | last post by:
Greetings! I thought I'd add a little something to a web site, a "tip of the week," and wanted it automated so that if I get hit by a truck (or, more likely, am forgetful), the tip is updated...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.