940 likes | 1.27k Vues
Perl/CGI. What We Have Learned and Will Learn. HTML/XHTML: contents CSS: display, layout JavaScript: client side programmability Ajax: based on JavaScript, XML, HTML, CSS Server side programmability CGI: C ommand G ateway I nterface PHP: Open source, strong database support
E N D
What We Have Learned and Will Learn • HTML/XHTML: contents • CSS: display, layout • JavaScript: client side programmability • Ajax: based on JavaScript, XML, HTML, CSS • Server side programmability • CGI: Command Gateway Interface • PHP: Open source, strong database support • ASP: Microsoft product • …
Command Gateway Interface (CGI) • CGI provides a way by which a web server can obtain data from (or send data to) a database, and other programs, and present that data to viewers via the web • A CGI program can be written in any programming language, but Perl is one of the most popular
Specifics About CGI • A CGI program can be written in any language that allows to be executed on the system: • C/C++ • Fortran • Perl • Python • VB • … • CGI programs are executable • Basically equivalent of letting the world run a program on your system • Security precautions: CGI programs need to reside in a special directory
Perl is A Common Choice • Simple, powerful string manipulation • Flexible pattern matching and substitution • Multi-platform deployment • Abstracted database access • Web server integration • Safe garbage collection • Simple integration with C/C++ for speed
Our First CGI Program – hello.cgi #!/usr/local/bin/perl –w print “Content-type: text/html\r\n\r\n”; print “<html>\n”; print “<head><title>Hello World!</title></head>”; print “<body style=\”text-align:center\”>\n”; print “<h1>Hello World!</h1>\n”; print “</body>”; print “</html>\n”; exit(0); http://www.csupomona.edu/~ftang/www/cgi-bin/hello.cgi http://www.csupomona.edu/cgi-user/ftang/www/cgi-bin/hello.cgi
Anatomy of the Program • First line “#!/usr/local/bin/perl –w” is known as the shebang, which informs the OS of the program that can understand this code (perl, in this case) • The MIME type “Content-type: text/html\r\n\r\n” tells the client’s browser how to interpret the information that follows the declaration • HTML document: the meat • Many possibilities • Return Value • exit (0): normal exit • exit (1): something bad happened
What can Perl Do? • What can Perl do? • print out HTML code • read a template and substitute portion of the template document with some dynamic code • connect to a database and dynamically generate HTML from query results • read in form variables and values from an HTTP request and compute the next HTML page using some process • Have Perl initiate an HTTP request to some other web server, mutate the results and pass them back to the client • … • Basically, if you can code it with Perl, then you can make it web-enabled with not much more effort
What is Perl? • Practical Extraction Report Language • Type “which perl” after we login, this will show us the path of the perl command • In our system, it is under “/usr/local/bin/perl” • You can write perl program and name it “filename.pl” • To run this perl program from terminal • try “perl filename.pl”
Basic Perl • Numeric and String Literals • Variables • Operators • Statements • Control Statements • Functions
Literal • A literal is a value that is hard-coded in your source code • Perl uses four types of literals: • Numbers: the most basic data type • Strings: a string is a series of characters that are handled as one unit • Arrays: an array is a series of numbers and strings handled as a unit; a list • Associative arrays: a list in which every value has an associated lookup item
Numeric Literals and String Literals • Numeric literals • integers such as 4, 043(octal format), 0x23(hex) • float such as .000034 or 3.4E-5 (scientific notation) • String literals (unlike C/C++, no ending NULL character) • Single-quoted ‘hello’ • Can add a line break by adding line break to source code • Double-quoted “hello” • Have special escape sequences \n, \% • Back-quoted `ls –l` • To execute system commands
Literals Continue… • Array literals • () an empty array • (“This”, “is”, “an”, “array”, “of”, “strings”) • Nesting arrays: • ((“Good”, “Morning”), (“Good”, “Afternoon”)) • Use a range of values • print (1..15) will print out numbers from 1 to 15 • print (“A”, “B”, “F”..”H”, “Z”) will print out A, B, F, G, H, Z • Will talk about associative array later
Variables (Three Types) • In Perl, you never have to declare, define, or allocate these data types • Scalar: holds one number or one string at a time • Always begin with a $ • $quantity = 5; $price = 10.3; $name = “blah” • Array: holds a list of values • Always begin with a @ • @emptyArr = ();@numArr=(1..10); @alphabet = (“A”..”Z”); • print @numArr; print $alphabet[25]; • using negative subscripts will print each array element in a reverse order • $numItems = @numArr; # assigns the number of elements in numArr to $numItems
Variable Continue… • Array continue: • How to grab a slice (a part) of an Array • @array = (“1”, “2”, “3”, “4”); • ($first, $third) = @array[0, 2]; • @half = @array[2, 3]; • Associative array: uses any value as an index into an array (like a hashtable) • Always begin with a % • %birthdays = (“Jack”, “Dec 2”, “Joe”, “June 2”, “Jane”, “Feb 13”); • print $birthdays{“Jack”}; • undefined key, return null or blank string
Some Array Functions • push() and pop(): • add or remove an item from the right hand side of an array • push(@mystack, $newvalue); • $off_val = pop(@mystack); • shift() and unshift() • like push() and pop(), but on the left hand side • reverse() • reverse the ordering of list • @a = (1, 2, 3}; @b = reverse(@a);
Some Array Functions • sort() • @x = sort (“small”, “medium”, “large”); • @y = sort(1, 32, 16, 4, 2); # gets (1, 16, 2, 32, 4) sorting is done on the string values of each number (alphabetical) • chop() • removes the last element from an array • chop(“small”, “medium”, “large”);
Associative Array Operators • keys(%arrayname) • lists all the key names in a specified associative array • @names = keys (%birthdays); • values(%arrayname); • returns the values of the specified associative array • @dates = values(%birthdays); • delete • deletes an associated key and value by key reference • delete $birthdays(“Joe”);
Operators • Binary arithmetic operators • +, -, *, /, %, ** • Unary arithmetic operators • +op1, -op1, ++op1, --op1, op1++, op1-- • Logical operators • && (and), || (or), ! (not) • Bitwise operators • & (and), | (or), ^ (exclusive-or), ~ (complement), >> (shift right), << (shift left) • Numeric relational operators • ==, !=, <, <=, >, >= • op1 <=> op2 • returns 1: if op1 is greater than op2 • returns 0: if op1 equals op2 • return –1: if op1 is less than op2
Operators, Continue… • String relational operators • op1 eq op2: returns true if equivalent • op1 ne op2: returns true if not equivalent • op1 lt op2: returns true if op1 is less than op2 • op1 le op2: returns true is op1 is less than or equal to op2 • op1 gt op2: returns true if op1 is greater than op2 • op1 ge op2: returns true if op1 is greater than or equal ot op2 • op1 cmp op2 • returns 1 if op1 is greater than op2 • returns 0 if op1 equals op2 • returns –1 if op1 is less than op2 • Ternary operator • condition-part ? true-part : false-part
Operators, Continue • Range operator .. • @array = (1..10); @array=(“aa”..“af”); • @array = (“ay”..“bf”); • String operators • Concatenation . • $first = “tom”; $second = “jerry”; • $name = $first . “ and ” . $second • Repetition x • $str = $str x 2; # print out the string twice • Assignment operators • var += op1; • …
Operators, Continue • Conversion between numbers and Strings • Scalar variables are converted automatically • $x = “40”; $y = “50”; • $z = $x + $y; # answer 90 • $s = $x.$y; # answer “4050” • chop() • takes a single argument and removes the last character from the string • chop(‘sandy’) would give ‘sand’ • most string inputs in Perl end with a \n. chop() can easily remove it for future processing
Operators, Continue • Assigning an array to scalar variables • @array = (“Tom Jones”, “123 Harley LN”, “Birmingham”, “AR”); • ($name, $street, $town, $state) = @array; • For operators, the order of precedence is always important • parentheses are recommended to explicitly tell Perl how and in which order to evaluate operators
So Far, We Have Learned … • Literals • Variables • Operators • Statement/Statement Block • Function • Reference
Statement and Statement Block • Statements are a complete unit of instruction for the computer to process • A statement block is a group of statements surrounded by curly braces • You can use the my() function to create variables whose scope is limited to the statement block $firstvar = 10; { my($firstvar) = “A”; print $firstvar x 5 . “\n”; } print (“firstvar = $firstvar\n”);
if/unless statement if (expression) { true_statement_1; …. true_statement_n; } if () {…} else {…} if () {…} elsif () {…} else {} unless ($age < 18) {print “Go and Vote\n”;} curly braces are required even if there’s only one statement
Loops • For statement • for (initial_expr; test_expr; increment_expr) {…} • while/until statement • while (expr) {…} # while the expr is true • until (expr) {…} # until the expr is false • foreach statement • foreach $i (@some_list) {…} @a = (1..5); foreach $i (reverse @a) { print $i; }
Jump Keywords • Four keywords • last: jumps out of the current statement block • next: skips the rest of the statement block and continues with the next iteration of the loop • redo: restarts the statement block • goto: jumps to a specified label { print(“What’s your name? ”); $name = <STDIN>; chop($name); # also try chomp() here if (!length($name)) { print(“Msg: Zero length input. Please try again\n”); redo; } print(“Thank you, ” . uc($name) . “\n”); # uc() uppercass }
Function • A function definition in Perl is as follows: sub functionName { } • Using parameters • All parameters to a function are stored in an array called @_. • Perl parameters are called by reference. Changing their values in the function also changes their values in the main program.
A Function Example $areaOfFirstCircle = areaOfCircle(5); print("$areaOfFirstCircle\n"); sub areaOfCircle { $radius = $_[0]; return(3.1415 * ($radius ** 2)); }
Using the Parameter Array (@_) • Perl lets you pass any number of parameters to a function. The function decides which parameters to use and in what order. firstSub(1, 2, 3, 4, 5, 6); firstSub(1..3); firstSub("A".."Z"); sub firstSub { $numParameters = @_ ; print("The number of parameters is $numParameters\n"); } areaOfRectangle(2, 3); areaOfRectangle(5, 6); sub areaOfRectangle { ($height, $width) = @_ ; $area = $height * $width; print("The height is $height. The width is $width. The area is $area.\n\n"); }
@array = (0..5); print("Before func call, array = @array\n"); firstSub(@array); print("After func call, array = @array\n"); sub firstSub { $_[0] = "A"; $_[1] = "B"; } @array = (0..5); print("Before func call, array = @array\n"); firstSub(@array); print("After func call, array = @array\n"); sub firstSub{ ($firstVar, $secondVar) = @_ ; $firstVar = "A"; $secondVar = "B"; } Passing Parameters by Reference • Perl parameters are called by reference, changing their values in the function also changes their values in the main program.
Scope of Variables • Scope refers to the visibility of variables. • Normally, every variable has a global scope. • Two Perl’s built-in functions can create variables of limited scope. • my() : visible to the current function only • local() : visible to the current function and any functions that are called by the current function • using my() enforces good programming practices and reduces headaches
Using a List as a Function Parameter • You can pass many scalar values as you want to a function, but only on an array. If you try to pass more than one array, the array elements become joined together and passed as one array to the function. • To pass both scalar and array, put scalar type first, and then array to distinguish them • Nested functions
String Functions • chomp (string or array): remove the trailing newline if it exists from a string or each element of an array • chop(string or array): removes the last character from a string or from every element in an array. The last character chopped is returned • index(string, substring, position): returns the position of the first occurrence of substring in string at or after position • rindex(string, substring, position) : returns the position of the last occurrence of substring in string at or after position • join(string, array): returns a string that consists of all the elements of array joined together by string
More String Functions • split(pattern, string, limit) • breaks up a string using pattern as the delimiter. • The limit parameter indicates how many parts to create from string • substr(string, offset, length) • returns a portion of a string as determined by the offset and length parameters. • lc(string), uc(string): lowercase; uppercase • lcfirst(string), ucfirst(string) • length(string) • Note as a general rule, if Perl sees a number where it expects a string, the number is automatically converted to a string • Note that some of the string functions use the special variable $_ as the default string to work with
Array Functions • defined(variable) • returns true if the variable has a real value and false if the variable has not yet been assigned a value • delete(key) • removes the key-value pair from the given associative array, e.g., delete($arr{“orange”}) • exists(key) • each(assoc_arr) • returns a list that contains key-value pairs from the associative array, so that it’s easy to iterate through • keys(assoc_arr) • returns a list of keys • values(assoc_arr) • returns a list of values
More Array Functions • pop, push, reverse, sort • shift/unshift: • shift(array): returns the first value of an array and reduces the size by one • unshift(arr1, arr2): adds the elements of arr2 to the front of arr1
Reference • A reference is a scalar value that points to a memory location that holds some type of data • All of your variables and functions are located at some memory location • References are used to hold the memory addresses • When a reference is dereferenced, you can retrieve the information referred to by the reference
Common Reference Types • $refScalar = \$scalar; • ${$refScalar} is a scalar value • $refArray = \@array; • @{$refArray} is an array value • $refHash = \%hash; • %{$refHash} is a hash value • $refRef = \$refScalar; • ${${$refRef}} is a scalar value • $refglob = \*file; • $refFunction = \&function; • &{$refFunction} is a function location
Passing Parameters to Functions @array1 = (1..5); @array2 = ("A".."E"); firstSub( \@array1, \@array2); sub firstSub { my($ref_firstArray, $ref_secondArray) = @_; print("The first array is @{$ref_firstArray}.\n"); print("The second array is @{$ref_secondArray}.\n"); } This program displays: The first array is 1 2 3 4 5. The second array is A B C D E.
The ref() Function • Using references to pass arrays into a function is easy • However, what if you pass a scalar reference to a function instead of an array reference? firstSub( 10, ("A".."E") ); sub firstSub { my($ref_firstArray, $ref_secondArray) = @_ ; print("The first array is @{$ref_firstArray}.\n"); print("The second array is @{$ref_secondArray}.\n"); } This program displays: Not an ARRAY reference at 08lst01.pl line 9.
The ref() Function (Con’t.) • ref() function can help you check the reference type before dereferencing a reference • ref(10); # return value is undefined • ref(\10); # return value is “SCALAR” • ref(\(1..10)); # return value is “ARRAY” • ref(\{1 => “Joe”}); # return value is “HASH” • ref(\&firstSub); # return value is “CODE” • ref(\\10); # return value is “REF” firstSub( 10, ("A".."E") ); sub firstSub { my($ref_firstArray, $ref_secondArray) = @_ ; print("The first array is @{$ref_firstArray}.\n") if (ref($ref_firstArray) eq "ARRAY"); print("The second array is @{$ref_secondArray}.\n" if (ref($ref_secondArray) eq "ARRAY"); }
Creating a Data Record • Associate arrays (hashes) are extremely useful to store information that facilitates easy retrieval %recordOne = ( "Name" => "Jane Hathaway", "Address" => "123 Anylane Rd.", "Town" => "AnyTown", "State" => "AnyState", "Zip" => "12345-1234“ ); %recordTwo = ( "Name" => "Kevin Hughes", "Address" => "123 Allways Dr.", "Town" => "AnyTown", "State" => "AnyState", "Zip" => "12345-1234" ); @database = ( \%recordOne, \%recordTwo ); print( %{$database[0]}->{"Address"} . "\n");
More on Perl Programming • Files (Input/Output) • Regular Expression • Special Variables
Using Files • Four basic operations: open, read, write, close • Three standard file handles: • STDIN: reads program input from the computer’s keyboard • STDOUT: displays program output to the computer’s monitor • STDERR: displays program errors, equivalent to STDOUT most of the time
Read From STDIN • Read a line of input from the standard input, STDIN • This will continue until you press Ctrl+D on Unix systems while (<STDIN>) { # diamond operators, default $_ print(); } # the following program acts the same as the above while ($tmpline = <STDIN>) { print ($tmpline); }
Using the Diamond Operators • If no file handle is used with the <>, Perl will examine the @ARGV special variable. If @ARGV has no elements, then the diamond operator will read from STDIN while (<>) { print(); } >perl file2.pl str1.pl str2.pl Perl will create the @ARGV array from the command line. Each file name on the command line – after the program will be added to the @ARGV array as an element. Normally, using <> to iterate over a list of filenames is handy.
File Functions • Open a file to read: $INPUT_FILE = "fixed.dat"; open(INPUT_FILE); @array = <INPUT_FILE>; close(INPUT_FILE); foreach (@array) { print(); } A file can be opened in many different ways: to read, write, append, etc. • Or: • open(FILE, “<fixed.dat”); • @array = <FILE>; • close(FILE); • foreach (@array) { • print(); • } • Or • open (FILE, “fixed.dat”) • || die “cannot open file”; • while (<FILE>) { • print $_; • } • close (FILE);
Examples • Call the open() function to open “message.log” file for writing with LOGFILE as the file handle if (open(LOGFILE, ">message.log")) { print LOGFILE ("This is message number 1.\n"); print LOGFILE ("This is message number 2.\n"); close(LOGFILE); } • Call the open() function to append to “message.log” if (open(LOGFILE, ">>message.log")) { print LOGFILE ("This is message number 3.\n"); close(LOGFILE); }