CS 205 Linux - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

CS 205 Linux

Description:

Understand the basics of the Perl language. Identify and use data types in ... print ('Greetings $namen'); Perl vs Bash Read vs STDIN. Bash Shell Scripts ... – PowerPoint PPT presentation

Number of Views:88
Avg rating:3.0/5.0
Slides: 16
Provided by: Gar158
Category:
Tags: greetings | linux

less

Transcript and Presenter's Notes

Title: CS 205 Linux


1
CS 205 - Linux
  • Chapter 9
  • Perl and CGI Programming

2
Objectives
  • Understand the basics of the Perl language.
  • Identify and use data types in Perl scripts.
  • Understand differences between the Awk program
    and Perl programming.
  • Access disk files in Perl.
  • Use Perl to sort information.
  • Set up a simple HTML Web page.
  • Understand how Perl and CGI are used for creating
    Web pages.
  • For the exam, you only need to know what Perl and
    CGI are.

Practice Exercises
None
Additional References
None
None.
3
Introduction to Perl
  • Perl Practical Extraction and Report Language
  • Free script language.
  • Runs on many operating systems
  • Examples UNIX, Linux, Windows, Mac OS X
  • Manipulates text, displays output, handles
    mathematical processes, and works with files.
  • Generating reports.
  • Used for Web programming.
  • Released by Larry Wall in 1987.
  • Interpreted language
  • Interpreter /usr/bin/perl
  • Sample Perl Program

Practice Exercises
Create the sample program exactly as indicated
and execute it.
Additional References
!/usr/bin/perl Program name
example1.pl print("This is a simple\n") print("Pe
rl program.\n")
None
Note the required 1st line to tell Linux to use
Perl and not the bash shell to execute the
commands. Also note the similarity and
differences between Perl and the awk command, C,
C, etc.
4
Perl vs Bash The
  • Bash Shell Scripts
  • Variables cannot begin with a .
  • To reference the value of a variable, you precede
    the variable with a , e.g. echo SHELL, echo
    PWD
  • Perl Scripts
  • Variables must begin with a .
  • To reference the value of the variable, just use
    the variable in a statement like print.

Practice Exercises
Create the sample program exactly as indicated
and execute it.
!/usr/bin/perl Program name example2.pl name
"Charlie" print ("Greetings name\n")
Additional References
None
Recall our earlier discussions. To learn a new
language, learn the syntax logic typically stays
the same. Note the use of the semicolon at the
end of each statement.
5
Perl vs Bash Read vs STDIN
  • Bash Shell Scripts
  • Use a read statement to read data from the
    keyboard, e.g. echo n Enter a number read
    num
  • Perl Scripts
  • Assign a variable to STDIN.

Practice Exercises
Create the sample program exactly as indicated
and execute it.
!/usr/bin/perl Program name example3.pl print
("Enter a number ") number ltSTDINgt print
("You entered number\n")
Additional References
None
Note the similarities in logic. Ask the user to
enter a number then read what they entered. The
format is very different but the logic is the
same.
6
Perl vs Bash If, Then, Else
  • Bash Shell Scripts
  • Use an if statement to perform a test and execute
    different blocks of logic based on the test being
    true or false. The then block of code is
    executed if true and the else block of code is
    executed if false. End the if statement with fi.
  • Perl Scripts
  • Use an if statement to perform a test and execute
    different blocks of logic based on the test being
    true or false. The true block is listed first in
    and the false block is listed second following
    an else with the code also in .

Practice Exercises
Create the sample program exactly as indicated
and execute it.
!/usr/bin/perl Program name example4.pl print
("Enter a number ") number ltSTDINgt if
(number 10) print ("That is the number I
was thinking of.\n") else print ("You
entered number\n")
Additional References
None
Note the use of the double equal sign for
comparison. Again, learn the syntax and the
language is easy to pick up.
7
Perl Operators
Practice Exercises
None.
Additional References
None
Note the virtually opposite use of operators from
bash shells, i.e. the Perl string operators are
the same as the bash shell numeric operators,
less the preceding dash.
8
Perl vs Bash Math
  • Bash Shell Scripts
  • Use a let statement to assign numeric values to
    variables to on the left of a mathematical
    expression.
  • Perl Scripts
  • The let statement is not used however, the rest
    of the statement is the same, with the addition
    of the semicolon at the end of the statements.

Practice Exercises
Create the sample program exactly as indicated
and execute it.
!/usr/bin/perl num1 10 num2 50 num3
12 average (num1 num2 num3) / 3 print
("The average is average\n")
Additional References
None
Virtually all scripting and programming languages
have the same rules for arithmetic expressions.
Multiplication and division before addition and
subtraction, unless enclosed in parenthesis.
9
Types of Variables
  • In Perl, data can be represented in a variety of
    ways
  • Variables and constants
  • Scalars
  • Numbers
  • Strings
  • Arrays
  • Hashes
  • Variables symbolic names that represent values
    stored in memory.
  • Constants do not change value as the program
    runs.
  • Scalar simple variable that holds a number or
    string.

Practice Exercises
None.
Additional References
None
The definitions for variables and constants is
the same across scripting and programming
languages.
10
Types of Variables
  • Numbers are stored inside the computer as
  • Signed integers (e.g., 14321)
  • Double-precision, floating-point values (e.g.,
    56.85)
  • Numeric literals are integers or floating-point
    values.
  • 0x used to express hexadecimal constants.
  • Strings are sequences of any types of characters.

Practice Exercises
None.
Additional References
None
Note that string variables are typically special
control characters.
11
Types of Variables
  • Arrays are variables that store an ordered list
    of scalar values.
  • An at sign (_at_) precedes the name of an array
    when assigning it values.
  • character used when processing the individual
    elements.

Practice Exercises
Try the script to see what happens.
!/usr/bin/perl _at_pets ("dog", "cat", "parrot",
"hamster" ) print ("My pets are\n") print
("pets0\n") print ("pets1\n") print
("pets2\n") print ("pets3\n")
Additional References
None
Arrays is one area where scripting and
programming language differ the most. Note the
unusual way that you can assign values to the
entries in an array. Also note the array begins
with entry 0.
12
Types of Variables
  • Hash variable that represents a set of key/value
    pairs.
  • Hash variables are preceded by when they are
    assigned values
  • animals (Tigers, 10, Lions, 20, Bears,
    30)
  • animals (Tigers gt 10, Lions gt 20, Bears
    gt 30)
  • To refer to an element, use before the variable
    name followed by the key in
  • animalsBears

Practice Exercises
Try the script to see what happens.
Additional References
None
What does Hash sound like a two dimensional
array?
13
Perl vs awk
  • Unlike Perl, Awk does not require programmer to
    explicitly set up looping structures.
  • Uses fewer lines of code to resolve
    pattern-matching extractions than Perl.
  • Similarities
  • Perl and Awk use to specify a comment line.
  • Pattern-matching code is the same in both
    programs.
  • Both are portable across many UNIX/Linux systems.

Practice Exercises
Try the script to see what happens.
Additional References
None
So, which do you prefer awk or perl?
14
Perl and CGI
  • Perl is the most commonly used language for
    Common Gateway Interface (CGI) programming.
  • CGI is a protocol governing how browsers and
    servers communicate.
  • Exchanging and processing a form containing
    information typically involves
  • Using CGI for communication between the clients
    Web browser and the Web server.
  • A program that can be executed
  • Often a Perl script or a program written in C.
  • Often stored in cgi-bin subdirectory on Web
    server.
  • Programs in cgi-bin are set up to have executable
    permissions
  • Also typically have r permissions so client can
    view associated Web page

Practice Exercises
None.
Additional References
None
Static web pages, like your instructors web
site, require manual changes. CGI is often used
for dynamic web sites however, a programming
language like Perl or C is helpful for complexity.
15
Summary
  • Perl is a scripting language.
  • Data types variables, constants, scalars,
    strings, arrays, and hashes.
  • Perl and Awk are both powerful processing
    languages that function in different ways.
  • HTML is used to format text in a Web page.
  • CGI is a protocol that governs how browsers and
    servers communicate.
Write a Comment
User Comments (0)
About PowerShow.com