basic variables & operators web programming1. review: perl basics syntax ► comments: start...

7
Basic Variables & Operators Web Programming 1

Upload: justin-mcdonald

Post on 19-Jan-2016

217 views

Category:

Documents


3 download

TRANSCRIPT

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

Basic Variables & Operators

Web Programming 1

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

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”);

► <STDIN>: 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

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

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

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

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 = <STDIN>;$name = “$first_name $last_name”;$a = 5; $b = $a + 3;

Web Programming 4

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

Working with numbers Arithmetic operators Example script

► add (+), subtract (-), multiply (*), divide (/)► mod (%), power (**)► increment (++, +=), decrement (--, -=)

• $a++; # increment $a by 1• $a+=2; # $a = $a+2;

Limitations Example script► limited number of digits can be stored► use a format

• e.g. printf (“%.2f”,3.3333);

Web Programming 5

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

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

Web Programming 6

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

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. \\, \”)

► \n newline\t tab\u change next character to uppercase\l change next character to lowercase\U change all following characters to uppercase\L change all following characters to lowercase\E end 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

Web Programming 7