Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 16 Introduction to Perl Perl (Practical Extraction and Report Language) Developed by Larry Wall as a Unix scripting language. Popular server-side.

Similar presentations


Presentation on theme: "Chapter 16 Introduction to Perl Perl (Practical Extraction and Report Language) Developed by Larry Wall as a Unix scripting language. Popular server-side."— Presentation transcript:

1 Chapter 16 Introduction to Perl Perl (Practical Extraction and Report Language) Developed by Larry Wall as a Unix scripting language. Popular server-side web development language.

2 Chapter 16 The First Perl Page This page uses Perl to create HTML tags.

3 Chapter 16 The First Perl Page #!/usr/local/bin/perl # File name: hello.pl.cgi print "Content-type: text/html", "\n\n"; print " ", "\n"; print " Web Hello \n"; print " \n"; print "Hello, World Wide Web! \n"; print "This is SAF\n"; print " ", "\n"; exit(0); This code creates the first Perl page.

4 Chapter 16 Perl Basics Perl uses a pound, #, to denote a comment. The comment ends when the line ends. A web page must indicate the content type: print "Content-type: text/html", "\n\n"; Perl programs should end with: exit(0);

5 Chapter 16 Perl Quotation Marks Double quotations: “ “. Variables and escape sequence are interpolated. Single quotations: ‘’ Variables and escape sequence are NOT interpolated. Back quotations: `` Command execution mode. Here Document: Quotations are not needed.

6 Chapter 16 Double Quotation Marks #!/usr/local/bin/perl $Title = "My First Web Page"; $Name = "Susan"; $Background = "white";... print "Hello, World Wide Web! \n"; print "This is $Name!\n"; Double quotation marks indicate that variable and escape sequences should be interpolated.

7 Chapter 16 Single Quotation Marks #!/usr/local/bin/perl $Title = "My First Web Page"; $Name = "Susan"; $Background = "white";... print ‘Hello, World Wide Web! \n’; print ‘This is $Name!\n’; Single quotation marks indicate that variable and escape sequences are NOT interpolated.

8 Chapter 16 Back Quotation Marks Back quotation marks are used for command execution.

9 Chapter 16 Back Quotations print "Echo Command: ", `echo My name is Susan`, " \n"; print "Word count(hello.pl.cgi): ”, “Lines, Words, Characters: \n"; print `wc hello.pl.cgi`, "\n"; print " ", "\n"; exit(0); This code executes Unix commands.

10 Chapter 16 Perl Scalar Variables Perl uses a $ to denote a scalar variable. Perl contains three types of scalars:  Numbers.  Strings.  References.

11 Chapter 16 Perl Arithmetic Operators Operator Meaning ++ -- Increment / Decrement - + Unary Minus/Unary Plus ** Exponentiation * / % Multiplication/Division/Modulus + - Addition / Subtraction = **= *= /= %=+= -= Assignment

12 Chapter 16 The Arithmetic Expressions Page

13 Chapter 16 Perl Arithmetic Functions Function Meaning abs(x) Absolute value. atan2(x) Arc tangent (in radians). chr(x) Character equivalent of x (ASCII). cos(x) Cosine of x (in radians). exp(x) e x. int(x) Integer equivalent of x (a float). log(x) log e x. rand(x) Random number between 0 and x (fraction). sin(x) Sine of x (in radians) sqrt(x) Square root of x.

14 Chapter 16 The Sales Tax Page The Sales Tax page uses the int() function.

15 Chapter 16 Perl String Operators Operator Meaning ++ String Increment =~ !~ Match/Does not match x Repetition. Concatenation = Assignment x= Assignment/Repetition.= Assignment/Concatenation

16 Chapter 16 The String Operators Page

17 Chapter 16 Perl String Functions Function Meaning index( string,substr ) Starting index of substr or -1. lc( expr ) Convert expr to all lowercase. lcfirst( expr ) Convert only first letter to lowercase. length( expr ) Return number of characters. ord( string ) Return ASCII value of first character. rindex( string, substr ) Starting index of substr from right or -1 uc( expr ) Convert expr to all uppercase. ucfirst( expr ) Convert only first letter to uppercase.

18 Chapter 16 The String Functions Page

19 $string = "O, Happy Dagger!"; print "String: $string \n"; $length = length($string); print "Length: $length \n"; $index1 = index($string, "Dagger", 0); print "Index of Dagger: $index1 \n"; $index1 = index($string, "z"); print "Index of z: $index1 \n"; $string = lc($string); print "Lowercase: $string \n"; $string = uc($string); print "Uppercase: $string \n";


Download ppt "Chapter 16 Introduction to Perl Perl (Practical Extraction and Report Language) Developed by Larry Wall as a Unix scripting language. Popular server-side."

Similar presentations


Ads by Google