Unformatted text preview:

Introduction to Programming the WWW I CMSC 10100 1 Summer 2004 Lecture 10 Today s Topics Perl data types and variables 2 Review Perl Data Types Scalars The simplest kind of data Perl can work with Either a string or number integer real etc Arrays of scalars An ordered sequence of scalars Associate arrays of scalars hash A set of key value pairs where the key is a text string to help to access the value 3 Review Perl Variables Variables are containers to store and access data in computer memory Variable names are not change in program The stored data usually changes during execution Three Variables in Perl Scalar variables hold a singular item such as a number for example 1 1239 12 or 123 or a string for example apple John Smith or address Array variables hold a set of items Hash variables hold a set of key value pairs 4 Review Scalar Variable and Assignment A scalar variable in Perl is always preceeded by the sign 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 Name of variable x 3 months 12 Variable s new value 5 Review Assigning New Values to Variables X 60 Weeks 4 X Weeks Assigns 4 to X and Weeks Note Perl variables are case sensitive x and 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 an underscore character Less than 251 characters Examples Valid baseball sum X Numb of bricks num houses and counter1 Not Valid 123go 1counter and counter 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 2 3 4 5 6 usr bin perl print Content type text html n n x 3 Assign 3 to x y 5 Assign 5 to y print The value of x is x print The value of y y 8 Would Output The Following 9 Basics of 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 Name of the function Comma separate each input argument to function function name argument1 argument2 argument3 Usually enclose arguments in parentheses 10 The print Function You can enclose output in parentheses or not When use double quotation marks Perl outputs the value of any variables For example x 10 print Mom please send x dollars Output the following message Mom please send 10 dollars 11 More on print If want to output the actual variable name and not its value then use single quotation marks x 10 print Mom please send x dollars Would output the following message Mom please send x dollars 12 Still More on print Support multiple arguments separated by comma Example x 5 print Send bucks need x No make that 5 x Outputs Send bucks need 5 No make that 25 13 String Variables Variables can hold numerical or character string data For example to hold customer names addresses product names and descriptions Assign abc letters abc fruit apple Assign apple Enclose in double quotes 14 String Variables The use of double quotes allows you to use other variables as part of the definition of a variable my stomach full full sentence My stomach feels my stomach print full sentence The value of my stomach is used as part of the full sentence variable 15 Quotation Marks Double quotation marks Allow variable interpolation and escape sequences Variable Interpolation any variable within double quotes will be replaced by its value when the string is printed or assigned to another variable Escape Sequences Special characters in Perl Not treated as characters if included in double quotes Can be turned to characters if preceeded by a backslash Other backslash interpretations Johnson pp 62 n new line t tab Double quotes examples 16 Quotation Marks Single quotation marks Marks are strictest form of quotes Everything between single quotes will be printed literally How to print a single quote inside of a single quotation marks Use backslash preceeding it Single quotes examples 17 Perl Operators Different versions of the operators for numbers and strings Categories Arithmetic operators Assignment operators Increment decrement operators concatenate operator and repeat operator Numeric comparison operators String comparison operators Logical operators 18 Arithmetic Operators Operator Effect Example Result Adds two items x 2 2 x is assigned 4 Subtraction y 3 y y 1 y is assigned 2 Division y 14 2 y is assigned 7 Multiplication z 4 y z 4 y is assigned 16 Exponent y 2 z 4 y z is assign 16 Remainder y 14 3 y is assigned 2 19 Example Program 1 usr local bin perl 2 print Content type text plain n n Assign 27 to cubed 3 cubed 3 3 Assign 4 onemore cubed 1 55 to 5 cubed cubed onemore cubed 6 remain onemore 3 7 print The value of cubed is cubed onemore onemore 8 print The value of remain remain remain is remainder of 28 3 or 1 20 Would Output The Following 21 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 42 or 17 depending on evaluation order 22 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 23 Review Generating HTML with Perl Script Use MIME type text html instead of text plain print Content type text html n n Add HTML codes in print print HTML HEAD TITLE Example TITLE HEAD Can use single quotes when output some HTML tags print FONT COLOR BLUE Can use backslash to signal that double quotation marks themselves should be output print FONT COLOR color 24 Variables with HTML Output II 1 usr local bin perl 2 print Content type text html n n 3 print HTML HEAD TITLE Example TITLE HEAD Set blue font size 5 4 print BODY FONT COLOR BLUE SIZE 5 5 num week 8 6 total day num week 7 7 num months num week 4 Assign 28 Assign 2 8 print Number of days are total day FONT 9 print HR The total number of months num months 10 print BODY HTML Horizontal rule followed by black font 25 Would Output The Following Run in Perl Builder 26 Assignment Operators Use the sign as an assignment operator to assign values to a variable Variable value Precede the sign with the


View Full Document

UChicago CMSC 10100 - Introduction to Programming the WWW I

Loading Unlocking...
Login

Join to view Introduction to Programming the WWW I and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Introduction to Programming the WWW I and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?