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.
Chapter 16 The First Perl Page This page uses Perl to create HTML tags.
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.
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);
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.
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.
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.
Chapter 16 Back Quotation Marks Back quotation marks are used for command execution.
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.
Chapter 16 Perl Scalar Variables Perl uses a $ to denote a scalar variable. Perl contains three types of scalars: Numbers. Strings. References.
Chapter 16 Perl Arithmetic Operators Operator Meaning Increment / Decrement - + Unary Minus/Unary Plus ** Exponentiation * / % Multiplication/Division/Modulus + - Addition / Subtraction = **= *= /= %=+= -= Assignment
Chapter 16 The Arithmetic Expressions Page
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.
Chapter 16 The Sales Tax Page The Sales Tax page uses the int() function.
Chapter 16 Perl String Operators Operator Meaning ++ String Increment =~ !~ Match/Does not match x Repetition. Concatenation = Assignment x= Assignment/Repetition.= Assignment/Concatenation
Chapter 16 The String Operators Page
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.
Chapter 16 The String Functions Page
$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";