Chapter 16 Introduction to Perl
Download
Report
Transcript Chapter 16 Introduction to Perl
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
This code creates the first Perl page.
#!/usr/local/bin/perl
# File name: hello.pl.cgi
print "Content-type: text/html", "\n\n";
print "<HTML> <HEAD>", "\n";
print "<TITLE> Web Hello</TITLE></HEAD>\n";
print "<BODY BGCOLOR = 'white'>\n";
print "Hello, World Wide Web!<BR>\n";
print "This is SAF\n";
print "</BODY></HTML>", "\n";
exit(0);
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
Double quotation marks indicate
that variable and escape sequences
should be interpolated.
#!/usr/local/bin/perl
$Title = "My First Web Page";
$Name = "Susan";
$Background = "white";
...
print "Hello, World Wide Web!<BR>\n";
print "This is $Name!\n";
Chapter 16
Single Quotation Marks
Single quotation marks indicate
that variable and escape sequences
are NOT interpolated.
#!/usr/local/bin/perl
$Title = "My First Web Page";
$Name = "Susan";
$Background = "white";
...
print ‘Hello, World Wide Web!<BR>\n’;
print ‘This is $Name!\n’;
Chapter 16
Back Quotation Marks
Back quotation marks are used
for command execution.
Chapter 16
Back Quotations
This code executes Unix commands.
print "Echo Command: ",
`echo My name is Susan`, "<P>\n";
print "Word count(hello.pl.cgi):<BR>”,
“Lines, Words, Characters:<BR>\n";
print `wc hello.pl.cgi`, "\n";
print "</BODY></HTML>", "\n";
exit(0);
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
abs(x)
atan2(x)
chr(x)
cos(x)
exp(x)
int(x)
log(x)
rand(x)
sin(x)
sqrt(x)
Meaning
Absolute value.
Arc tangent (in radians).
Character equivalent of x (ASCII).
Cosine of x (in radians).
ex.
Integer equivalent of x (a float).
logex.
Random number between 0 and x (fraction).
Sine of x (in radians)
Square root of x.
Chapter 16
The Sales Tax Page
The Sales Tax page uses the int() function.
Chapter 16
Perl String Operators
Operator
++
=~ !~
x
.
=
x=
.=
Meaning
String Increment
Match/Does not match
Repetition
Concatenation
Assignment
Assignment/Repetition
Assignment/Concatenation
Chapter 16
The String Operators Page
Chapter 16
Perl String Functions
Function
index(string,substr)
lc(expr)
lcfirst(expr)
length(expr)
ord(string)
rindex(string, substr)
uc(expr)
ucfirst(expr)
Meaning
Starting index of substr or -1.
Convert expr to all lowercase.
Convert only first letter to lowercase.
Return number of characters.
Return ASCII value of first character.
Starting index of substr from right
or -1
Convert expr to all uppercase.
Convert only first letter to
uppercase.
Chapter 16
The String Functions Page
Chapter 16
The String Functions Page
$string = "O, Happy Dagger!";
print "String: $string<BR>\n";
$length = length($string);
print "Length: $length<BR>\n";
$index1 = index($string, "Dagger", 0);
print "Index of Dagger: $index1<BR>\n";
$index1 = index($string, "z");
print "Index of z: $index1<BR>\n";
$string = lc($string);
print "Lowercase: $string<BR>\n";
$string = uc($string);
print "Uppercase: $string<BR>\n";