Spring Semester 2005


Circular scripts (Part II).

19. Let's now move to the second problem, with one (a suddenly simple program).

#!/usr/bin/perl

&printTop; print "Hello!"; &printBottom; 

sub printTop {
  print qq{Content-type: text/html\n\n<html>
    <head><title>My Pictures Script</title></head>
    <body bgcolor=white>
  }; 
} 

sub printBottom {
  print qq{</body></html>}; 
}
20. So far so good.

21. What do we add in two to it?

#!/usr/bin/perl

&printTop; 

$name = $ENV{"SCRIPT_NAME"}; 

$method = $ENV{"REQUEST_METHOD"};

print "Hello, I am  $name, and I am called with method: $method. <p>"; 

&printBottom; 

sub printTop {
  print qq{Content-type: text/html\n\n<html>
    <head><title>My Pictures Script</title></head>
    <body bgcolor=white>
  }; 
} 

sub printBottom {
  print qq{</body></html>}; 
}
22. This is three a bit reorganized for better focus.

#!/usr/bin/perl

&printTop; $name = $ENV{"SCRIPT_NAME"}; $method = $ENV{"REQUEST_METHOD"};
print "Hello, I am  $name, and I am called with method: $method. <p>"; 

print qq{
  <form method="$method" action="$name"> 
     Argument: <input type="text" name="arg"> <p>
     Function: <select name="fun"> 
       <option value="non"> Click Me!
       <option value="add"> Addition
       <option value="sub"> Subtraction 
     </select> <p>
     <input type="submit" value="Proceed">      
  </form>
}; 

&printBottom; 

sub printTop {
  print qq{Content-type: text/html\n\n<html>
    <head><title>My Pictures Script</title></head>
    <body bgcolor=white>
  }; 
} 

sub printBottom {
  print qq{</body></html>}; 
}
23. Let's make a change (four) and experiment.

#!/usr/bin/perl

&printTop; $name = $ENV{"SCRIPT_NAME"}; $method = $ENV{"REQUEST_METHOD"};
print "Hello, I am  $name, and I am called with method: $method. <p>"; 

print "(", $ENV{"QUERY_STRING"}, ")<p>"; 

print qq{
  <form method="$method" action="$name"> 
     Argument: <input type="text" name="arg"> <p>
     Function: <select name="fun"> 
       <option value="non"> Click Me!
       <option value="add"> Addition
       <option value="sub"> Subtraction 
     </select> <p>
     <input type="submit" value="Proceed">      
  </form>
}; 

&printBottom; 

sub printTop {
  print qq{Content-type: text/html\n\n<html>
    <head><title>My Pictures Script</title></head>
    <body bgcolor=white>
  }; 
} 

sub printBottom {
  print qq{</body></html>}; 
}
24. Is your calculator working already?

25. Are we getting any closer?

26. Perhaps we should take a break?

27. Nope, we should keep going.

28. Here's five, taking the input apart.

#!/usr/bin/perl

&printTop; $name = $ENV{"SCRIPT_NAME"}; $method = $ENV{"REQUEST_METHOD"};
print "Hello, I am  $name, and I am called with method: $method. <p>"; 

$input = $ENV{"QUERY_STRING"}; 

print "($input)<p>"; 

@pairs = split(/&/, $input); 

foreach $pair (@pairs) {
  print "($pair)<p>"; 
} 

print qq{
  <form method="$method" action="$name"> 
     Argument: <input type="text" name="arg"> <p>
     Function: <select name="fun"> 
       <option value="non"> Click Me!
       <option value="add"> Addition
       <option value="sub"> Subtraction 
     </select> <p>
     <input type="submit" value="Proceed">      
  </form>
}; 

&printBottom; 

sub printTop {
  print qq{Content-type: text/html\n\n<html>
    <head><title>My Pictures Script</title></head>
    <body bgcolor=white>
  }; 
} 

sub printBottom {
  print qq{</body></html>}; 
}
29. But that's only part of it.

30. The next one (six) does the rest of the work.

#!/usr/bin/perl

&printTop; $name = $ENV{"SCRIPT_NAME"}; $method = $ENV{"REQUEST_METHOD"};
print "Hello, I am  $name, and I am called with method: $method. <p>"; 

$input = $ENV{"QUERY_STRING"}; 

print "($input)<p>"; 

@pairs = split(/&/, $input); 

foreach $pair (@pairs) {
  print "($pair)<p>"; 
  ($nam, $val) = split(/=/, $pair); 
  print qq{
    Name is ($nam) and value is ($val). <p> 
  }
} 

print qq{
  <form method="$method" action="$name"> 
     Argument: <input type="text" name="arg"> <p>
     Function: <select name="fun"> 
       <option value="non"> Click Me!
       <option value="add"> Addition
       <option value="sub"> Subtraction 
     </select> <p>
     <input type="submit" value="Proceed">      
  </form>
}; 

&printBottom; 

sub printTop {
  print qq{Content-type: text/html\n\n<html>
    <head><title>My Pictures Script</title></head>
    <body bgcolor=white>
  }; 
} 

sub printBottom {
  print qq{</body></html>}; 
}
31. The next program (seven) not only takes the input apart in the same manner as six does, it also carefully records the input in a hash table, for future use, and gets prepared for its role as a calculator.

#!/usr/bin/perl

&printTop; $name = $ENV{"SCRIPT_NAME"}; $method = $ENV{"REQUEST_METHOD"};
print "Hello, I am  $name, and I am called with method: $method. <p>"; 

$input = $ENV{"QUERY_STRING"}; 

print "($input)<p>"; 

@pairs = split(/&/, $input); 

foreach $pair (@pairs) {
  print "($pair)<p>"; 
  ($nam, $val) = split(/=/, $pair); 
  print qq{
    Name is ($nam) and value is ($val). <p> 
  };
  $form{$nam} = $val; 
} 

foreach $key (keys %form) {
  print "($key) is associated with (", $form{$key}, ")<p>"; 
} 

if ($form{fun} eq "add") {

} elsif ($form{fun} eq "sub") {

} else {
  
} 

print qq{
  <form method="$method" action="$name"> 
     Argument: <input type="text" name="arg"> <p>
     Function: <select name="fun"> 
       <option value="non"> Click Me!
       <option value="add"> Addition
       <option value="sub"> Subtraction 
     </select> <p>
     <input type="submit" value="Proceed">      
  </form>
}; 

&printBottom; 

sub printTop {
  print qq{Content-type: text/html\n\n<html>
    <head><title>My Pictures Script</title></head>
    <body bgcolor=white>
  }; 
} 

sub printBottom {
  print qq{</body></html>}; 
}
32. Are we getting any closer?

#!/usr/bin/perl

&printTop; $name = $ENV{"SCRIPT_NAME"}; $method = $ENV{"REQUEST_METHOD"};
print "Hello, I am  $name, and I am called with method: $method. <p>"; 
$input = $ENV{"QUERY_STRING"}; print "($input)<p>"; 

@pairs = split(/&/, $input); 

foreach $pair (@pairs) {
  ($nam, $val) = split(/=/, $pair); 
  $form{$nam} = $val; 
} 

if ($form{fun} eq "add") {
  $acc = $acc + $form{arg}; 
} elsif ($form{fun} eq "sub") {
  $acc = $acc - $form{arg};
} else {
  
} 

print qq{
  <form method="$method" action="$name"> 
     Accumulator is: ($acc) <p> 
     Argument: <input type="text" name="arg"> <p>
     Function: <select name="fun"> 
       <option value="non"> Click Me!
       <option value="add"> Addition
       <option value="sub"> Subtraction 
     </select> <p>
     <input type="submit" value="Proceed">      
  </form>
}; 

&printBottom; 

sub printTop {
  print qq{Content-type: text/html\n\n<html>
    <head><title>My Pictures Script</title></head>
    <body bgcolor=white>
  }; 
} 

sub printBottom {
  print qq{</body></html>}; 
}
33. The program above (eight) is trying. What's missing?

34. How's nine handling this problem?

#!/usr/bin/perl

&printTop; $name = $ENV{"SCRIPT_NAME"}; $method = $ENV{"REQUEST_METHOD"};
print "Hello, I am  $name, and I am called with method: $method. <p>"; 
$input = $ENV{"QUERY_STRING"}; print "($input)<p>"; 

@pairs = split(/&/, $input); 

foreach $pair (@pairs) {
  ($nam, $val) = split(/=/, $pair); 
  $form{$nam} = $val; 
} 

$acc = $form{acc}; 

if ($form{fun} eq "add") {
  $acc = $acc + $form{arg}; 
} elsif ($form{fun} eq "sub") {
  $acc = $acc - $form{arg};
} else {
  
} 

print qq{
  <form method="$method" action="$name"> 
     Accumulator is: ($acc) <p>
     Field named acc containg the accumulator value: 
         <input type="text" value="$acc" name="acc"> <p>  
     Argument: <input type="text" name="arg"> <p>
     Function: <select name="fun"> 
       <option value="non"> Click Me!
       <option value="add"> Addition
       <option value="sub"> Subtraction 
     </select> <p>
     <input type="submit" value="Proceed">      
  </form>
}; 

&printBottom; 

sub printTop {
  print qq{Content-type: text/html\n\n<html>
    <head><title>My Pictures Script</title></head>
    <body bgcolor=white>
  }; 
} 

sub printBottom {
  print qq{</body></html>}; 
}
35. In ten we change one word only. Did you see which one?

#!/usr/bin/perl

&printTop; $name = $ENV{"SCRIPT_NAME"}; $method = $ENV{"REQUEST_METHOD"};
print "Hello, I am  $name, and I am called with method: $method. <p>"; 
$input = $ENV{"QUERY_STRING"}; print "($input)<p>"; 

@pairs = split(/&/, $input); 

foreach $pair (@pairs) {
  ($nam, $val) = split(/=/, $pair); 
  $form{$nam} = $val; 
} 

$acc = $form{acc}; 

if ($form{fun} eq "add") {
  $acc = $acc + $form{arg}; 
} elsif ($form{fun} eq "sub") {
  $acc = $acc - $form{arg};
} else {
  
} 

print qq{
  <form method="$method" action="$name"> 
     Accumulator is: ($acc) <p>
     Field named acc containg the accumulator value: 
         <input type="hidden" value="$acc" name="acc"> <p>  
     Argument: <input type="text" name="arg"> <p>
     Function: <select name="fun"> 
       <option value="non"> Click Me!
       <option value="add"> Addition
       <option value="sub"> Subtraction 
     </select> <p>
     <input type="submit" value="Proceed">      
  </form>
}; 

&printBottom; 

sub printTop {
  print qq{Content-type: text/html\n\n<html>
    <head><title>My Pictures Script</title></head>
    <body bgcolor=white>
  }; 
} 

sub printBottom {
  print qq{</body></html>}; 
}
36. Can you see which one, and what the difference is?

37. What is the difference between GET and POST?

The program below (eleven) would be able to point it out to you.

#!/usr/bin/perl

&printTop; $name = $ENV{"SCRIPT_NAME"}; $method = $ENV{"REQUEST_METHOD"};
print "Hello, I am  $name, and I am called with method: $method. <p>"; 

if ($method eq "GET") {
  $input = $ENV{"QUERY_STRING"}; 
} else { # else what? 
  read(STDIN, $input, $ENV{"CONTENT_LENGTH"}); 
}

print "($input)<p>"; 

@pairs = split(/&/, $input); 

foreach $pair (@pairs) {
  ($nam, $val) = split(/=/, $pair); 
  $form{$nam} = $val; 
} 

$acc = $form{acc}; 

if ($form{fun} eq "add") {
  $acc = $acc + $form{arg}; 
} elsif ($form{fun} eq "sub") {
  $acc = $acc - $form{arg};
} else {
  
} 

print qq{
  <form method="POST" action="$name"> 
     Accumulator is: ($acc) <p>
     Field named acc containg the accumulator value: 
       <input type="hidden" value="$acc" name="acc"> <p>  
     Argument: <input type="text" name="arg"> <p>
     Function: <select name="fun"> 
       <option value="non"> Click Me!
       <option value="add"> Addition
       <option value="sub"> Subtraction 
     </select> <p>
     <input type="submit" value="Proceed">      
  </form>
}; 

&printBottom; 

sub printTop {
  print qq{Content-type: text/html\n\n<html>
    <head><title>My Pictures Script</title></head>
    <body bgcolor=white>
  }; 
} 

sub printBottom {
  print qq{</body></html>}; 
}
38. Are we any closer now?

39. Can you write the two programs?

40. I am sure you can.

41. If you have any questions please let me know!

42. But you know what: I want to make sure I have a question for you.

43. What, if you don't mind, is the difference between eleven and ten?

(Take a look at their URLs when you work with them. Where does the input go?)

44. Let me know.


Last updated: Jan 22, 2005 by Adrian German for A348/A548