1 / 68

Web Wizard’s Guide to CGI/Perl

Web Wizard’s Guide to CGI/Perl. David Lash Chapter 3 Perl Basics. Chapter Objectives. Describe Perl scalar variables for numerical and string data Describe Perl conditional statements Learn how to use Perl functions Use a function to send data to your program. Using Scalar Variables.

Télécharger la présentation

Web Wizard’s Guide to CGI/Perl

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Web Wizard’s Guide to CGI/Perl David Lash Chapter 3 Perl Basics

  2. Chapter Objectives • Describe Perl scalar variables for numerical and string data • Describe Perl conditional statements • Learn how to use Perl functions • Use a function to send data to your program.

  3. Using Scalar Variables • Variables allow you to store and access data in computer memory. • Two primary types: • Scalar variables - hold a singular item such as a number (for example, 1, 1239.12, or –123) or a character string (for example, “apple,” “ John Smith,” or “address”). • List variables - hold a set of items (such as a set of numbers). (More in Chapter 5).

  4. Assigning Values to Variables • Place the variable’s name on the left side of an equals sign (=) and the value on the right side of the equals sign. • The following Perl statements use two variables: $x and $months

  5. Assigning New Values to Variables $X = 60; $Weeks = 4; $X = $Weeks; • Assigns 4 to $X and $Weeks. • Note: Perl variables are case sensitive. • $xand $X are considered different variable names.

  6. Selecting Variable Names • Perl variable rules • Perl variable names must have a dollar sign ($) as the first character. • The second character must be a letter or underscore (_). • Less than 251 characters. • Valid: $baseball, $_sum, $X, $Numb_of_bricks, $num_houses, and$counter1. • Not Valid: $123go, $1counter, andcounter.

  7. Variables and the print • Place a variable name inside the double quotes of the print statement. to print out the value. E.g., • print “The value of x= $x”; 1. #!/usr/bin/perl 2. print “Content-type: text/html\n\n”; 3. $x = 3; 4. $y = 5; 5. print “The value of x is $x. ”; 6. print “The value of y= $y.”; Assign 3 to $x Assign 5 to $x

  8. Would Output The Following:

  9. Operating on Variables • Perl expressions are used to manipulate data values. • Use operators such as a plus sign (+) for addition and a minus sign (–) for subtraction.For example, 1. #!/usr/bin/perl 2. print “Content-type: text/html\n\n”; 3. $x = 3 + 4; 4. $y = 5 + $x; 5. print “The value of x is $x but y = $y”; Assign 7 to $x Assign 12 to $y

  10. Would Output The following:

  11. Some Key Perl Operators

  12. Example Program 1. #!/usr/bin/perl 2. print “Content-type: text/html\n\n”; 3. $cubed = 3 ** 3; 4. $onemore = $cubed + 1; 5. $cubed = $cubed + $onemore; 6. $remain = $onemore % 3; 7. print “The value of cubed is $cubed onemore= $onemore ”; 8. print “The value of remain= $remain”; Assign 27 to $cubed Assign 55 to $cubed $remain is remainder of 28 / 3 or 1

  13. Would Output The Following ...

  14. Writing Complex Expressions • Operator precedence rules define the order in which the operators are evaluated. • For example, consider the following expression: • $x = 5 + 2 * 6; • $x could = 56 or 17 depending on evaluation order

  15. Perl Precedence Rules 1. Operators within parentheses. 2. Exponential operators. 3. Multiplication and division operators. 4. Addition and subtraction operators. Consider the following $X = 100 – 3 ** 2 * 2; $Y = 100 – ((3 ** 2) * 2); $Z = 100 – ( 3 ** (2 * 2) ); Evaluates to 82 Evaluates to 19

  16. Variables with HTML Output - II 1. #!/usr/bin/perl 2. print “Content-type: text/html\n\n”; 3. print “<HTML> <HEAD> <TITLE> Example </TITLE></HEAD>”; 4. print “<BODY> <FONT COLOR=BLUE SIZE=5>”; 5. $num_week = 8; 6. $total_day = $num_week * 7; 7. $num_months = $num_week / 4; 8. print “Number of days are $total_day </FONT>”; 9. print “<HR>The total number of months=$num_months”; 10. print “</BODY></HTML>”; Set blue font, size 5 Assign 28 Assign 2 Horizontal rule followed by black font.

  17. Would Output The Following ...

  18. String Variables • Variables can hold numerical or character string data • For example, to hold customer names, addresses, product names, and descriptions. $letters=”abc”; $fruit=”apple”; Assign “abc” Assign “apple” Enclose in double quotes

  19. String Variables • String variables have their own operations. • You cannot add, subtract, divide, or multiply string variables. • The concatenate operator joins two strings together and takes the form of a period (“.”). • The repeat operator is used when you want to repeat a string a specified number of times.

  20. Concatentate Operator • Joins two strings together (Uses period (“.”)). $FirstName = “Bull”; $LastName = “and Bear”; $FullName1 = $FirstName . $LastName; $FullName2 = $FirstName . “ “ . $LastName; print “FullName1=$FullName1 and Fullname2=$FullName2”; • Would output the following: FullName1=Bulland Bear and FullName2=Bull and Bear • Note …can use double quotation marks $Fullname2 = “$FirstName $LastName”; • Has the same effect as $Fullname2 = $FirstName . “ “ . $LastName;

  21. Repeat Operator • Used to repeat a string a number of times. Specified by the following sequence: $varname x 3 • For example, $score = “Goal!”; $lots_of_scores = $score x 3; print “lots_of_scores=$lots_of_scores”; • Would output the following: lots_of_scores=Goal!Goal!Goal! Repeat string value 3 times.

  22. A Full Program Example ... 1. #!/usr/bin/perl 2. print "Content-type: text/html\n\n"; 3. print "<HTML> <HEAD><TITLE> String Example</TITLE></HEAD>"; 4. print "<BODY>"; 5. $first = "John"; 6. $last = "Smith"; 7. $name = $first . $last; 8. $triple = $name x 3; 9. print "<BR> name=$name"; 10.print "<BR> triple = $triple"; 11. print "</BODY></HTML>"; Concatenate Repeat

  23. Would Output The Following ...

  24. Conditional Statements • Conditional statements enable programs to test for certain variable values and then react differently • Use conditionals in real life: • Get on Interstate 90 East at Elm Street and go east toward the city. If you encounter construction delays at mile marker 10, get off the expressway at this exit and take Roosevelt Road all the way into the city. Otherwise, stay on I-90 until you reach the city.

  25. Conditional Statements • Perl supports 3 conditional clauses: • Anifstatement specifies a test condition and set of statements to execute when a test condition is true. • Anelsifclause is used with an if statement and specifies an additional test condition to check when the previous test conditions are false. • Anelseclause is used with an ifstatementand possibly an elsif clause. It specifies a set of statements to execute when one or more test conditions are false.

  26. The if Statement • Uses a test condition and set of statements to execute when the test condition is true. • A test condition uses a test expression enclosed in parentheses within an ifstatement. • When the test expression evaluates to true, then one or more additional statements within the required curly brackets ({ … }) are executed.

  27. Numerical Test Operators

  28. Numerical Test Operators

  29. A Sample Conditional Program 1. #!/usr/bin/perl 2. print "Content-type: text/html\n\n"; 3. print "<HTML> <HEAD><TITLE> String Example </TITLE></HEAD>"; 4. print "<BODY>"; 5. $grade = 92; 6. if ( $grade > 89 ) { 7. print “<FONT COLOR=BLUE> Hey you got an A.</FONT><BR> ”; 8. } 9. print “Your actual score was $grade”; 10. print “</BODY></HTML>”;

  30. Would Output The Following ...

  31. Different Values of Line 5

  32. String Test Operators • Perl supports a set of string test operators that are based on ASCII code values. • Standard, numerical representation of characters. • Every letter, number, and symbol translates into a code number. • “A” is ASCII code 65, and “a” is ASCII code 97. • Numbers are lower ASCIIcode values than letters, uppercase letters lower than lowercase letters. • Letters and numbers are coded in order, so that the character “a” is less than “b”, “C” is less than “D”, and “1” is less than “9”.

  33. String Test Operators

  34. The elsif Clause • Specifies an additional test condition to check when all previous test conditions are false. • Used only with if statement • When its condition is true, gives one or more statements to execute

  35. The elsif Clause 1. #!/usr/bin/perl 2. print “Content-type: text/html\n\n”; 3.$grade = 92; 4.if ( $grade > 100 ) { 5. print “Illegal Grade > 100”; 6.} 7.elsif ( $grade > 89 ){ 8. print “Hey you got an A ”; 9.} 1.0 print “Your actual grade was $grade”;

  36. The elsif Clause 1. #!/usr/bin/perl 2.print “Content-type: text/html\n\n”; 3.$grade = 92; 4.if ( $grade >= 100 ) { 5. print “Illegal Grade > 100 ”; 6.} 7.elsif ( $grade < 0 ) { 8. print “illegal grade < 0 ”; 9.} 10.elsif ( $grade > 89 ){ 11. print “Hey you got an A ”; 12.} 13. print “Your actual grade was $grade”;

  37. Some Sample Values for Line 3

  38. The else Clause • Specifies a set of statements to execute when all other test conditions in anifblock are false. • It must be used with at least one if statement, (can also be used with an if followed by one or several elsif statements.

  39. Using An Else Clause 1.#!/usr/bin/perl 2.print “Content-type: text/html\n\n”; 3.$grade = 92; 4.if ( $grade >= 100 ) { 5. print “Illegal Grade > 100”; 6.} 7.elsif ( $grade < 0 ) { 8. print “illegal grade < 0”; 9.} 10.elsif ( $grade > 89 ){ 11. print “Hey you got an A”; 12.} 13.else { 14. print “Sorry you did not get an A”; 15.}

  40. Sample Values of Line 3 Line 3 Output $grade=92; Hey you got an A $grade=89; Sorry you did not get an A $grade=40; Sorry you did not get an A $grade=1100; Illegal Grade > 100 $grade=-50; illegal grade < 0

  41. Using Perl Functions • Perl includes built-in functions that provide powerful additional capabilities to enhance your programs. • Work much like operators, except that most (but not all) accept one or more arguments (I.e., input values into functions).

  42. Will Cover 3 Sets of Functions • Will discuss several functions • Some basic Perl functions—the square root, absolute value, length, and random number generation functions. • The print function—more details about the capabilities of the print function. • The param function—use of this function to receive input into your programs.

  43. Some Basic Perl Functions • Here are a few functions within Perl • sqrt() –a single numerical argument as input & returns the square root of the argument passed in.For example, $x=25; $y=sqrt($x); print “x=$x y=$y and finally ”, sqrt(144); would output the following: x=25 y=5 and finally 12

  44. Some Basic Perl Functions - abs() • Absolute Value - • abs() – accepts a single numerical argument & returns the absolute value of this argument. For example, $x=-5; $y=42; print abs($x), “ “, abs($y); • would output the following output: 5 42 • The extra space in the print line (“ ”) provides a space between the output values.

  45. Some Basic Perl Functions -rand() • rand() – generates arandom number from 0 to the number passed into it. • Example use: simulating a roll of a die or displaying a random image in a document. • When int() is used with rand(), it forces rand() to return whole numbers instead of its default fractional numbers. • For example, $numb = int( rand(3) ); • returns a random number that is either a 0, 1, or 2.

  46. Some Basic Perl Functions - rand() • Here is another example of the rand() function: $dice=int(rand(6))+1; print "Your random dice toss is $dice"; • The random number that is generated in this case can be a 1, 2, 3, 4, 5, or 6. Thus one possible output of this code is Your random dice toss is 6

  47. Some Basic Perl Functions - rand() • length() – The length function is used to work with string variables. It returns the number of characters in the string argument. For example, $name = “smith”; $title = “Domestic Engineer”; print “name is ”, length($name), “ title is ”, length($title), “ characters long”; • returns the following output: name is 5 title is 17 characters long

  48. Some Basic Perl Functions - rand() • localtime() – The localtime function is typically used with the time() function to determine the current date and time while your program is executing. • time returns the number of seconds since January 1, 1970. • When time() is used as an argument to the localtime() function, the output will be a set of scalar variables that provide the current date and time information. • For example,

  49. locatltime(time) return values ($sec, $min, $hr, $day, $mon, $yr, $wkday, $DayNumOfYr, $TZ ) = localtime(time); Current second Current minute Current Hour Day Of Month Month (0 is Jan, 1 is Feb, etc) Day of week (0 is Sun, 1 is Mon, since 1900. Number of years since 1900. Day of year (0 is Jan1, 1 is Jan 2, etc.) Timezone 1 if local time is using Daylight Savings Time; 0 otherwise

More Related