TC392 Week 3 Programs

Note: Programs run on penguin can start the first line with the following (shorter) perl path: #!/usr/bin/perl
  • w3.1.pl The "if" and "elsif" statements #!/usr/bin/perl #w3.1.pl statement blocks; if statement print ("How many hotdogs would you like? "); chomp($no= <STDIN>); if ($no <= 0){ print ("So you are the vegetarian.\n"); } elsif ($no <= 2){ print ("We can handle $no. \n"); } elsif ($no >=3){ print ("We can put more on. "); print ("By the way, you won't mind\nif we keep the ambulance handy.\n"); } __END__
  • w3.2.pl A simple while statement. #!/usr/bin/perl #w3.2.pl while statement @people = (Jim, Floyd, Fred, Jane, Marta, Jill, Sam, Joe, Alice, Andrea); while (@people) { $person = pop(@people); print("$person\n"); } __END__
  • w3.3.pl A more complex while statement #!/usr/bin/perl #w3.3.pl while statement (with last). @people = (Jim, Floyd, Fred, Jane, Marta, Jill, Sam, Joe, Alice, Andrea); print("Please enter your name: "); chomp($name = <STDIN>); while (@people) { $try = pop(@people); if ($try eq $name){ print("Yes, you can use our computer.\n"); $success = 1; last; } #End if statement } #End while if (!$success){ #The ! negates as in != print("Sorry $name, you are not on my list.\n"); } __END__
  • w3.4.pl a complete for statement #!/usr/bin/perl #w3.4.pl the for statement @people = (Jim, Floyd, Fred, Jane, Marta, Jill, Sam, Joe, Alice, Andrea); print("Please enter your name: "); chomp($name = <STDIN>); for ($a=0;$a <= $#people; $a++) { # $#people counts @people # print ("Last person is at sub $#people\n"); # print ("DEBUG: $people[$a]\n"); #DEBUG if ($people[$a] eq $name){ print("Yes, you can use our computer.\n"); $success = 1; last; } #End if block } #End for block if (!$success){ print("Sorry $name, you are not on my list.\n"); } __END__
  • w3.5.pl Things can be easier with foreach #!/usr/bin/perl #w3.5.pl foreach() @people = (Jim, Floyd, Fred, Jane, Marta, Jill, Sam, Joe, Alice, Andrea); print("Please enter your name: "); chomp($name = <STDIN>); foreach (@people){ # print ("Check $_\n"); #DEBUG if ($_ eq $name){ print("Yes, $_, you can use our computer.\n"); $success = 1; last; } #End if block } #End for block if (!$success){ print("Sorry, $name, you are not on my list.\n"); } __END__
  • w3.6.cgi: Import a text file. Add <p> to all blank lines. Put an HTML header on the file so that a browser can display it. #!/usr/bin/perl #w3.6.cgi open (FH, "sample.txt"); while (<FH>){ #puts each line from FH in $_ @lines = (@lines, $_); } ##Print the HTML header print("Content-type: text/html\n\n"); print <<EOT <html> <head> <title> Tc392 w3.6.cgi </title> </head> <body bgcolor="#ffffe9"> <h1> Instant HTML Paragraphs</h1> EOT ; foreach (@lines){ if ($_ eq "\n"){ $_ = "<p>\n"; } print ("$_"); } print("</body></html>\n"); __END__
  • w3.8.cgi if and elsif statements; using split with assignment #!/usr/bin/perl # w3.8.cgi A different background color for each day of the week. #PART 1: Print the header. print ("Content-type: text/html\n\n"); print <<EOT <html> <head> <title>Penguin</title> </head> EOT ; #PART 2: Determine background color by Day of week chomp($date= `date`); # Date returns (for instance): Wed Feb 5 15:40:44 EST 1997 ($today, $month, $dayno, $time, $zone, $year)= split(/ +/, $date); if ($today eq "Sun") {print ("<body bgcolor=\"#ff0000\" text=\"#ffffff\">\n");} elsif ($today eq "Mon") {print("<body bgcolor=\"#ff3333\" text=\"#ffffff\">\n");} elsif ($today eq "Tue") {print ("<body bgcolor=\"#ff7777\" text=\"#ffffff\">\n");} elsif ($today eq "Wed") {print ("<body bgcolor=\"#eeff22\" text=\"#000000\">\n");} elsif ($today eq "Thu") {print ("<body bgcolor=\"#ffCCCC\" text=\"#000000\">\n");} elsif ($today eq "Fri") {print ("<body bgcolor=\"#ffDDDD\" text=\"#000000\">\n");} else {print ("<body bgcolor=\"#ffffff\" text=\"#000000\">\n");} #Part 3: Print the rest of the page print <<EOT <center><h1> A Horse of a Differnet Color. </h1></center> Note: This page uses a different background color for each day of the week.<p> EOT ; print ("Today is <b>$today $month $dayno, $year</b> <p>\n"); print("</body></html>\n"); __END__