Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 *Copyright © 2002 Pearson Education, Inc.. 2 Web Wizard’s Guide to CGI/Perl David Lash Chapter 3 Perl Basics.

Similar presentations


Presentation on theme: "1 *Copyright © 2002 Pearson Education, Inc.. 2 Web Wizard’s Guide to CGI/Perl David Lash Chapter 3 Perl Basics."— Presentation transcript:

1 1 *Copyright © 2002 Pearson Education, Inc.

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

3 3 *Copyright © 2002 Pearson Education, Inc. Chapter Objectives l Describe Perl scalar variables for numerical and string data l Describe Arithmetic Operations l Describe Perl conditional statements

4 4 *Copyright © 2002 Pearson Education, Inc. Using Scalar Variables l Variables allow you to store and access data in computer memory. l 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).

5 5 *Copyright © 2002 Pearson Education, Inc. Assigning Values to Variables l 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

6 6 *Copyright © 2002 Pearson Education, Inc. $X = 60; $Weeks = 4; $X = $Weeks; l Assigns 4 to $X and $Weeks. l Note: Perl variables are case sensitive. » $x and $X are considered different variable names. Assigning New Values to Variables

7 7 *Copyright © 2002 Pearson Education, Inc. Selecting Variable Names l 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, and counter.

8 8 *Copyright © 2002 Pearson Education, Inc. 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

9 9 *Copyright © 2002 Pearson Education, Inc. Would Output The Following:

10 10 *Copyright © 2002 Pearson Education, Inc. Operating on Variables l 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

11 11 *Copyright © 2002 Pearson Education, Inc. Would Output The following:

12 12 *Copyright © 2002 Pearson Education, Inc. Some Key Perl Operators

13 13 *Copyright © 2002 Pearson Education, Inc. 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

14 14 *Copyright © 2002 Pearson Education, Inc. Would Output The Following...

15 15 *Copyright © 2002 Pearson Education, Inc. Writing Complex Expressions l Operator precedence rules define the order in which the operators are evaluated. l For example, consider the following expression: » $x = 5 + 2 * 6; » $x could = 56 or 17 depending on evaluation order

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

17 17 *Copyright © 2002 Pearson Education, Inc. Variables with HTML Output - II 1. #!/usr/bin/perl 2. print “Content-type: text/html\n\n”; 3. print “ Example ”; 4. print “ ”; 5. $num_week = 8; 6. $total_day = $num_week * 7; 7. $num_months = $num_week / 4; 8. print “Number of days are $total_day ”; 9. print “ The total number of months=$num_months”; 10. print “ ”; Assign 28 Assign 2 Set blue font, size 5 Horizontal rule followed by black font.

18 18 *Copyright © 2002 Pearson Education, Inc. Would Output The Following...

19 19 *Copyright © 2002 Pearson Education, Inc. String Variables l 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

20 20 *Copyright © 2002 Pearson Education, Inc. String Variables l String variables have their own operations. » You cannot add, subtract, divide, or multiply string variables. l 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.

21 21 *Copyright © 2002 Pearson Education, Inc. 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”; l Would output the following: FullName1=Bulland Bear and FullName2=Bull and Bear

22 22 *Copyright © 2002 Pearson Education, Inc. Repeat Operator l Used to repeat a string a number of times. Specified by the following sequence: $varname x 3 l For example, $score = “Goal!”; $lots_of_scores = $score x 3; print “lots_of_scores=$lots_of_scores”; l Would output the following: lots_of_scores=Goal!Goal!Goal! Repeat string value 3 times.

23 23 *Copyright © 2002 Pearson Education, Inc. Repeat W/o Repeat Operator l You don’t have to use the repeat operator. Also common to see … l For example, $score = “Goal!”; $lots_of_scores = “$score$score$score” print “lots_of_scores=$lots_of_scores”; l Would output the following: lots_of_scores=Goal!Goal!Goal! Repeat string value 3 times.

24 24 *Copyright © 2002 Pearson Education, Inc. A Full Program Example Using Repeat Operator... 1. #!/usr/bin/perl 2. print "Content-type: text/html\n\n"; 3. print " String Example "; 4. print " "; 5. $first = "John"; 6. $last = "Smith"; 7. $name = $first. $last; 8. $triple = $name x 3; 9. print " name=$name"; 10.print " triple = $triple"; 11. print " "; Concatenate Repeat

25 25 *Copyright © 2002 Pearson Education, Inc. Would Output The Following...

26 26 *Copyright © 2002 Pearson Education, Inc. Conditional Statements l Conditional statements enable programs to test for certain variable values and then react differently l 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.

27 27 *Copyright © 2002 Pearson Education, Inc. Conditional Statements l Perl supports 3 conditional clauses: » An if statement specifies a test condition and set of statements to execute when a test condition is true. » An elsif clause is used with an if statement and specifies an additional test condition to check when the previous test conditions are false. » An else clause is used with an if statement and possibly an elsif clause. It specifies a set of statements to execute when one or more test conditions are false.

28 28 *Copyright © 2002 Pearson Education, Inc. The if Statement l 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 if statement. » When the test expression evaluates to true, then one or more additional statements within the required curly brackets ( { … } ) are executed.

29 29 *Copyright © 2002 Pearson Education, Inc. Numerical Test Operators

30 30 *Copyright © 2002 Pearson Education, Inc. A Sample Conditional Program 1. #!/usr/bin/perl 2. print "Content-type: text/html\n\n"; 3. print " String Example "; 4. print " "; 5. $grade = 92; 6. if ( $grade > 89 ) { 7. print “ Hey you got an A. ”; 8. } 9. print “Your actual score was $grade”; 10. print “ ”;

31 31 *Copyright © 2002 Pearson Education, Inc. Would Output The Following...

32 32 *Copyright © 2002 Pearson Education, Inc. Different Values of Line 5 1. #!/usr/bin/perl 2. print "Content-type: text/html\n\n"; 3. print " String Example "; 4. pri n t " "; 5. $grade = 92; 6. if ( $grade > 89 ) { 7. print “ Hey you got an A. ”; 8. } 9. print “Your actual score was $grade”; 10. print “ ”;

33 33 *Copyright © 2002 Pearson Education, Inc. String Test Operators l 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 ASCII code 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”.

34 34 *Copyright © 2002 Pearson Education, Inc. String Test Operators

35 35 *Copyright © 2002 Pearson Education, Inc. The elsif Clause l 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

36 36 *Copyright © 2002 Pearson Education, Inc. 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”;

37 37 *Copyright © 2002 Pearson Education, Inc. 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”;

38 38 *Copyright © 2002 Pearson Education, Inc. Some Sample Values for Line 3 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”;

39 39 *Copyright © 2002 Pearson Education, Inc. The else Clause Specifies a set of statements to execute when all other test conditions in an if block 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.

40 40 *Copyright © 2002 Pearson Education, Inc. 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.}

41 41 *Copyright © 2002 Pearson Education, Inc. 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 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.}

42 42 *Copyright © 2002 Pearson Education, Inc. Summary l Variables are used to store and access data in computer memory. l Numerical and string variables have unique operations that can be used to manipulate their values. l Conditional statements are used to test for conditions and, based on the results of the test, execute specific program statements. Perl provides different test condition operators for string and numerical variables.

43 43 *Copyright © 2002 Pearson Education, Inc. Summary The if statement can be used by itself and is a basic way of testing conditions. The elsif clause can be used with the if statement to provide another test condition when the original if statement is false. An else clause provides statements that execute when all other if and elsif conditions are false.

44 44 *Copyright © 2002 Pearson Education, Inc. Perl “Jeoprdy” Answer: $1st_counter, $2ndctr, squared ? l Question: Give an example of 3 illegal variable names. l Question: What are the Perl operator precedence order? Answer: exponential operators, parenthesis, multiplication & division, addition & subtraction

45 45 *Copyright © 2002 Pearson Education, Inc. So think your smart? l Answer: (a) if, elsif, else (b) if l Question: Which three types of conditional clauses were described in this chapter. Which is the only one that can be used by itself? l Question: Which test operator is used to test whether one of two string variables is greater than the other? What test operator is used to test whether one of two numerical variables is greater than the other? Answer: gt, >

46 46 *Copyright © 2002 Pearson Education, Inc. Impressive but... l Answer: “Independence forever” l Question: What did John Adams say in 1826 when asked if he had a message to pass along to people on the 50th anniversary of the signing of the declaration of Independence.


Download ppt "1 *Copyright © 2002 Pearson Education, Inc.. 2 Web Wizard’s Guide to CGI/Perl David Lash Chapter 3 Perl Basics."

Similar presentations


Ads by Google