Identifying Data Types

Download Report

Transcript Identifying Data Types

Chapter Nine
Perl and CGI Programming
Lesson A
Learning to Use Perl
2
Objectives
Understand the basics of the Perl
language
Identify and use data types in Perl scripts
Understand the difference between the
Awk program and Perl programming
3
Objectives
Create simple Perl scripts with variables
and logic structures
Create simple Perl scripts to read and sort
data files
4
Introduction to Perl
Perl contains a
features found in
other languages –
it is very similar to
the C and also
contains features
found in Awk and
shell programs
5
Introduction to Perl
Perl can be
directed to read its
input from the
keyboard
6
Introduction to Perl
7
Introduction to Perl
8
Identifying Data Types
Data may be represented in Perl in a
variety of ways:
– Variables and constants
– Scalars
– Numbers
– Strings
– Arrays
– Hashes
9
Identifying Data Types
Variables are symbolic names that
represent values stored in memory
– The value of a variable can change while the
program runs; constants do not change
Scalars are simple variables that hold a
number or a string
– Scalar variable names begin with a dollar sign
($)
10
Identifying Data Types
Numbers are stored as either signed
integers, or as double-precision floatingpoint values
– Numeric literals can be either integers or
floating-point values
– Perl uses added convention with numeric
literals to improve legibility: the underscore
character (_)
11
Identifying Data Types
Strings are sequences of any types of
characters
– Strings are often used for logical analysis,
sorts, or searches
– String literals are usually delimited by either
single (‘) or double quotes (“)
12
Identifying Data Types
13
Identifying Data Types
14
Identifying Data Types
The use of special
codes determined
the output of this
Perl script
15
Identifying Data Types
Arrays are variables that store an ordered
list of scalar values that are accessed with
numeric subscripts
– An at sign (@) precedes the name of an array
when assigning it values
– Use the dollar sign ($) when processing the
individual elements of an array
16
Identifying Data Types
Arrays are variables that represent a set of
key/value pairs
– A percent sign (%) precedes the name of a
hash variable when assigning it a value
– Use the dollar sign ($) to refer to a single
element of a hash
17
Perl versus the Awk Program
The Awk program
uses fewer lines of
code to resolve
pattern-matching
extractions than
does Perl
18
How Perl Accesses Disk Files
Perl uses
filehandles to
reference files, as
shown in this
figure
19
Using Perl to Sort
You can sort words
in a Perl program
into alphabetical
order using the
sort function
20
Using Perl to Sort
Perl can access a
file by passing the
filename on the
command line
21
Using Perl to Sort
You can sort
numeric fields in a
Perl program by
using a sort
subroutine
22
Lesson B
Creating an Interactive Web Page
23
Objectives
Set up an HTML Web page
Use Perl and CGI scripts to make your
web pages interactive
Use X Window and Netscape to retrieve
Web pages
24
Setting Up a Web Page
Web pages can be created using HTML
(Hypertext markup Language)
HTML is a format for creating documents with
embedded codes known as tags and when the
document is viewed in a Web browser, the tags
give the document special properties
After using HTML to create a Web page, the
page is published on a Web server, this allows
others to access the document via the Internet
25
Creating a Web Page
HTML documents are created by typing its
text and the desired embedded tags
There are two parts to the HTML code
– The head contains the title, which appears on
the top bar of the browser window
– The body defines what appears within the
browser window
26
Creating Web Pages
An HTML
document viewed
in Netscape
Navigator
27
28
CGI Overview
CGI (Common Gateway Interface) is a protocol,
or set of rules, governing how browsers and
servers communicate
Scripts that send or receive information from a
server need to follow the CGI protocol
Perl is the most commonly used language for
CGI programming
Perl scripts are written to get, process, and
return information through Web pages
29
30
31
32
33
34
35
Chapter Summary
Perl is used as a powerful text-manipulation
tool similar to the Awk program
Perl is written in scripts that are translated and
executed by the Perl program
The Perl programmer has to write processhandling instructions for data items to prevent
misidentification of data types and subsequent
processing errors
Perl has three basic data types: scalars,
arrays, and hashes
36
Chapter Summary
A list is an ordered group of simple variables or
literals, separated by commas
Anything besides a textual sort must be
handled with a sort subroutine
An HTML document contains two parts: a head
and a body
CGI is a protocol or set of rules governing how
browsers and servers communicate
To run your Web pages, you need to be in X
Window and have access to a Web browser
37