#!/usr/bin/perl use CGI; # this is used for getting the form values my $q = new CGI; my $num1 = $q->param('first'); my $num2 = $q->param('second'); my $operation = $q->param('oper'); #$theuser = '' unless $theuser; print "Content-type: text/html\n\n"; print "\n"; print "\n"; print "A perl script \n"; print "<\HEAD>\n"; print "\n"; print "This is a demo of a perl script\n"; print "Print the date inside a table:"; print "
\n"; print "
\n"; print `date`; print "
\n"; print "
"; print `echo "I will now do a WC on testscript.pl which will print the wc of this file to the screen. I said in class that you could do cat on this file, and you can, but there are issues regarding new lines. So I am linking to the text version of this instead. Note that to run the script in your own directory, you will have to rename it testscript.pl not textscript.txt"`; print "
Here are the results of word count: \n
"; print `wc testscript.pl`; print "
"; print qq( See the code \n); print "
By the way, a good website that I have been looking at for some of these examples is http://www.comp.leeds.ac.uk/Perl/basic.html \n"; print "
Now I am going to call a C program called helloworld
\n"; print `./helloworld`; print "
"; print "The C-code is simple enough, but here is is regardless. Note that the exe is ./helloworld and it is chmodded to 700"; print "\n helloworld.c \n"; print "
\n"; print qq(Here is a sample form with submit, etc. Note that one could have made the form on a different page than the action went to, but it is possible to be the same page
\n); print qq(
); print qq(
\n); print qq( Enter two numbers and whether you want to ADD or MULTIPLY them\n); print qq(
); print qq(\n); print qq(\n); print qq(\n); print qq(\n); print qq(
); print "Your first number is: "; print qq($num1); print "

"; print "Your second number is: "; print qq($num2); print "

"; print "Your operation is : "; print qq($operation); print qq(

); print qq(Result is: ); if ($operation eq "MULTIPLY") { my $result = $num1 * $num2; print "$result
"; } if ($operation eq "ADD") { my $result = $num1 + $num2; print "$result
"; } print "
"; print "

lastly, demonstrating arrays and hash lists (look at the code file for this part... First arrays:\n"; print "
"; @food = ("apples", "pears", "bananas"); print "
Original array : @food \n
"; print "First value in 2nd spot: $food[2]
"; #add an element to the array. This will add the element to the END of the array push(@food, "eggs"); #note that in order to insert in the middle, you have to remove all the elements #one at a time from the end, insert the new element, and then put all the #elements back that you removed. This suggests using a 2nd array for temporary storage. push(@temp, pop(@food)); # remove last element = eggs push(@temp, pop(@food)); # remove last element = bananas push(@food, "tomatoes"); # insert element tomatoes push(@food, pop(@temp)); # insert last element of temp = bananas push(@food, pop(@temp)); # insert bananas back. print "
New array is: @food\n"; print "
The 2nd element is : $food[2] \n"; print "
Here is the hash example: Note some people call these associative arrays instead of hashes \n
"; # Note that if you modify the following hash array, it will not altar your actual grade %comp206grades = ("Daniel", "C", "Joe" , 59, "Bob" , 90 , "Alice", "NOT DETERMINED YET" ); print "
here is the grade that the student accessed by key \"Daniel\" got: \n"; print qq( $comp206grades{"Daniel"}); print "\n"; print "\n"; #note that my public_html directory is chmoded to 711 #cgi-bin is inside public_html and is chmodded to 711 #this file (testscript.pl) is chmodded to 700. This seems weird (to put 700 not 711), but the SOCS Servers have some special program that allows you to chmod it to 700 to get it to work