Presentation is loading. Please wait.

Presentation is loading. Please wait.

Basic Variables & Operators Web Programming1. Review: Perl Basics Syntax ► Comments: start with # (ignored by Perl) ► Statements: ends with ; (performed.

Similar presentations


Presentation on theme: "Basic Variables & Operators Web Programming1. Review: Perl Basics Syntax ► Comments: start with # (ignored by Perl) ► Statements: ends with ; (performed."— Presentation transcript:

1 Basic Variables & Operators Web Programming1

2 Review: Perl Basics Syntax ► Comments: start with # (ignored by Perl) ► Statements: ends with ; (performed by Perl) ► print: writes to standard output print “My name is $name\n”; print (“My name is ”,$name,”\n”); ► : reads 1 line from standard input Running Perl ► perl [-switch] program_name ► program_name #!/usr/bin/perl at the first line chmod 755 filename Web Programming 2

3 Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g. @array1 = (“Mary”,”Tom”); ► holds a list of scalars ► $array1[0]=“Mary”; $array1[1]=“Tom”; Associative array (i.e. hash) ► e.g. %hash1 = (“Mary”,”F”,”Tom”,”M”); ► holds key-value scalar pairs ► $hash1{“Mary”}=“F”; $hash1{“Tom”}=“M”; Web Programming 3

4 Scalar Variable: syntax Syntax ► first character must be $. ► second character must be a letter. ► can consist of only letters, digits, or underscore ► case-sensitive ► can be as long as you want (within reason). Assigning values to scalar variables ► $first_name = “Sam”; $last_name = ; $name = “$first_name $last_name”; $a = 5; $b = $a + 3; Web Programming 4

5 Working with numbers Arithmetic operators  Example script Example script ► add (+), subtract (-), multiply (*), divide (/) ► mod (%), power (**) ► increment (++, +=), decrement (--, -=) $a++; # increment $a by 1 $a+=2;# $a = $a+2; Limitations  Example script Example script ► limited number of digits can be stored ► use a format e.g. printf (“%.2f”,3.3333); Web Programming 5

6 Working with strings String operators ► concatenation: $name = “Kiduk”. “ ”; $name.= “Yang”;# $name == “Kiduk Yang” ► repeat: $line= “-” x 10; # $line == “----------” String functions ► length($string); ► substr($string,offset,length) returns a substring of $string starting at offset for length $lastname= substr($name,6,4); ► chop($string) cuts the last character of a string ► chomp ($string) cuts the last character only if it is a line break  Example script Example script Web Programming 6

7 Scalar Variable: advanced Escape sequences ► consists of backslash (\) and a character ► must be inside double quotes to use backslash or double quote inside double quotes, precede it with a backslash (i.e. \\, \”) ► \nnewline \ttab \uchange next character to uppercase \lchange next character to lowercase \Uchange all following characters to uppercase \Lchange all following characters to lowercase \Eend the effect of \U, \L Initial value of a scalar variable is null. Character and numeric scalars are interchangeable. ► $a=1; $a=“1”; ► null value is converted to zero in arithmetic operations.  Example script Example script Web Programming 7


Download ppt "Basic Variables & Operators Web Programming1. Review: Perl Basics Syntax ► Comments: start with # (ignored by Perl) ► Statements: ends with ; (performed."

Similar presentations


Ads by Google