New page in perl

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JWest46088
    New Member
    • Sep 2006
    • 74

    #1

    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:

    Code:
    #!/usr/bin/perl
    
    BEGIN
    {
    	open(STDERR, ">&STDOUT");
    	select(STDERR); $| = 1;
    	select(STDOUT); $| = 1;
    	print "Content-type: text/html\n\n";
    }
    
    if($ENV{'QUERY_STRING'} eq "")
    {
    	&page1;
    	exit;
    }
    
    read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
    
    if($buffer eq "")
    {
    	$buffer = $ENV{'QUERY_STRING'};
    }
    
    @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;
        	$name =~ tr/+/ /;
        	$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    
    	if($FORM{$name} eq "")
    	{
    		$FORM{$name} = $value;
    	}
    	else
    	{
    		$FORM{$name} .= " " . $value;
    		#$FORM{$name} /= " " . $value;
    
    	}
    }
    
    $num = $FORM{"num"};
    
    sub page1
    {
    print <<ENDHTML;
    <title>Assignment #4 (CS Credit)</title>
    <body text="white" bgcolor="blue">
    <font face="arial" size="4">
    <div align="center">
    <form method="get" action="/~westj2/cgi-bin/assignment4_CS_credit.cgi">
    <br>
    
    Enter the number of elements to add and average:
    <input type="text" name="num" value="3"><br><br>
    <input type="submit" name="sub" value="Submit">
    <input type="reset" name="res" value="Reset">
    
    ENDHTML
    }
    
    print <<ENDHTML;
    
    <title>Assignment #4 (CS Credit)</title>
    <body text="white" bgcolor="blue">
    <font face="arial" size="4">
    <div align="center">
    <form method="get" action="/~westj2/cgi-bin/assignment4_CS_credit.cgi">
    
    Enter elements you want to add and average:<br><br>
    
    ENDHTML
    
    for($i = 1; $i <= $num; $i++)
    {
    	print "Element #$i: ";
    	print "<input type='text' name=$i value=$i><br>";
    }
    
    
    print <<ENDHTML;
    
    <br><input type="submit" name="calc" value="Submit">
    <input type="reset" name="res" value="Reset">
    <input type="hidden" name="num" value=$FORM{"num"}>
    
    ENDHTML
    
    my @queue;
    
    for($i = 1; $i <= $num; $i++)
    {
    	unshift(@queue, $FORM{"$i"});
    }
    
    my @elements = @queue;
    
    print "You entered: ";
    
    for($i = 1; $i <= $num; $i++)
    {
    	print(pop(@queue));
    	print " ";
    }
    
    my $sum;
    
    for($i = 0; $i < $num; $i++)
    {
    	$sum = $sum + @elements[$i];
    }
    
    print "<br>Sum: $sum";
    
    my $avg = $sum / $num;
    print "<br>Average: $avg";
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    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.

    Code:
    #!/usr/bin/perl
     
    BEGIN {
    
        open(STDERR, ">&STDOUT");
        select(STDERR); $| = 1;
        select(STDOUT); $| = 1;
        print qq{Content-type: text/html\n\n
    <title>Assignment #4 (CS Credit)</title>
    <body text="white" bgcolor="blue">
    <font face="arial" size="4">
    <div align="center">
    };
    }
    
    my $url = '/~westj2/cgi-bin/assignment4_CS_credit.cgi';
     
    read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
    $buffer = $ENV{'QUERY_STRING'};
    
    @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;
       $name =~ tr/+/ /;
       $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
       if($FORM{$name} eq ""){
          $FORM{$name} = $value;
       }
       else{
          $FORM{$name} .= " " . $value;
       }
    }
     
    $num = $FORM{"num"};
    if($FORM{'page'} == 2) {
       page2();
       exit;
    }
    elsif($FORM{'page'} == 3) {
       page3();
       exit;
    }
    else {
       page1();
       exit;
    }
    
    sub page1 {
       print <<ENDHTML;
    <form method="get" action="$url">
    <input type="hidden" name="page" value=2>
    <br>
     
    Enter the number of elements to add and average:
    
    <input type="text" name="num" value="3"><br><br>
    <input type="submit" name="sub" value="Submit">
    <input type="reset" name="res" value="Reset">
    </form>
     
    ENDHTML
    }
    
    sub page2 {
       print <<ENDHTML;
    <form method="get" action="$url">
    <input type="hidden" name="page" value=3>
     
    Enter elements you want to add and average:<br><br>
     
    ENDHTML
     
    for($i = 1; $i <= $num; $i++)
    {
        print "Element #$i: ";
        print "<input type='text' name=$i value=$i><br>";
    }
     
     
    print <<ENDHTML;
     
    <br><input type="submit" name="calc" value="Submit">
    <input type="reset" name="res" value="Reset">
    <input type="hidden" name="num" value=$FORM{"num"}>
    </form>
     
    ENDHTML
    }
    
    sub page3 { 
       my @queue;
     
       for($i = 1; $i <= $num; $i++){
          unshift(@queue, $FORM{"$i"});
       }
       my @elements = @queue;
       print "You entered: ";
       for($i = 1; $i <= $num; $i++){
          print(pop(@queue));
          print " ";
       }
       my $sum;
       for($i = 0; $i < $num; $i++){
          $sum = $sum + $elements[$i];
       }
       print "<br>Sum: $sum";
       my $avg = $sum / $num;
       print "<br>Average: $avg";
       print qq{<br><br><form action="$url">
    <input type="submit" value="Try Again...">
    </form>
    }; 
    }

    Comment

    • JWest46088
      New Member
      • Sep 2006
      • 74

      #3
      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.
      Last edited by JWest46088; Jun 3 '09, 02:37 PM. Reason: Added information

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        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.

        Comment

        • JWest46088
          New Member
          • Sep 2006
          • 74

          #5
          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.



          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.

          Comment

          • RonB
            Recognized Expert Contributor
            • Jun 2009
            • 589

            #6
            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

            Comment

            • JWest46088
              New Member
              • Sep 2006
              • 74

              #7
              Doh! Thanks Ron and Kevin. It works now.

              Comment

              • KevinADC
                Recognized Expert Specialist
                • Jan 2007
                • 4092

                #8
                Originally posted by RonB
                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
                Yikes... I think this guy is stalking me! ;)

                Comment

                • RonB
                  Recognized Expert Contributor
                  • Jun 2009
                  • 589

                  #9
                  Originally posted by KevinADC
                  Yikes... I think this guy is stalking me! ;)
                  What should I do when I catch you! :) :)

                  Comment

                  • KevinADC
                    Recognized Expert Specialist
                    • Jan 2007
                    • 4092

                    #10
                    Originally posted by RonB
                    What should I do when I catch you! :) :)

                    hmm.... never thought about that. Buy me a Margarita? ;)

                    Comment

                    • RonB
                      Recognized Expert Contributor
                      • Jun 2009
                      • 589

                      #11
                      You know that I know the perfect place.
                      http://www.rosysattheb each.com

                      Now, watch me get dinged for the semi self promotion

                      Comment

                      • KevinADC
                        Recognized Expert Specialist
                        • Jan 2007
                        • 4092

                        #12
                        hehehe.... maybe one of these days I'll take you up on that. BTW, welcome to bytes.com :)

                        Comment

                        • RonB
                          Recognized Expert Contributor
                          • Jun 2009
                          • 589

                          #13
                          Originally posted by KevinADC
                          hehehe.... maybe one of these days I'll take you up on that.
                          I might even get behind the bar, which usually means I lower their profit margin, so beware. :)

                          Originally posted by KevinADC
                          BTW, welcome to bytes.com :)
                          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".

                          Comment

                          • KevinADC
                            Recognized Expert Specialist
                            • Jan 2007
                            • 4092

                            #14
                            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.

                            Comment

                            • NeoPa
                              Recognized Expert Moderator MVP
                              • Oct 2006
                              • 32669

                              #15
                              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).

                              Comment

                              Working...