SlideShare ist ein Scribd-Unternehmen logo
1 von 55
Downloaden Sie, um offline zu lesen
CS 3430: Introduction to Python and Perl
Lecture 15
Department of Computer Science
Utah State University
Overview
●
Introduction To Perl
●
Installation, Editing, Documentation
●
Running Perl programs
●
Numbers and strings
●
Control Structures
Perl Overview
Learning Objectives
●
Familiarity, not competence
●
Online resources
●
Basic coding principles
History
●
Larry Wall began to develop Perl in 1987
●
The language was originally named Pearl, but there
was a language by that name
●
Perl stands for “Practical Extraction and Report
Language”
●
Rumor has it that the name Perl came first and the
expansion came later
Perl Principles
●
There is more than one way to do it
●
Perl: Swiss Army chainsaw of programming
languages
●
No unnecessary limits
●
Perl is the duct tape of the Internet (not as true
as it used to be anymore)
What Perl is Used For
●
Internet and Web Scripting
●
Component Integration
●
Database Programming
●
Rapid Prototyping
●
Data Mining
Perl's Strengths
●
Free
●
Portable
●
Mixable
●
Object-oriented
Perl's Weaknesses
● Slower than C/C++
● Long and rather steep learning curve
● Code can be difficult to read
 Joke: Perl is a “write only” language
 Hard even for the original programmer to
read later
Influences on Perl
● AWK
● C/C++
● Lisp
● Pascal
● Unix shell
● BASIC-PLUS
Resources
● http://www.perl.org - site for everything
that is Perl – documentation, IDEs, tutorials,
links to helpful resourcesa
● http://cpan.perl.org - Comprehensive
Perl Archive Network – a lot of helpful Perl tools
and third-party libraries
Installation, Editing, Documentation
Perl Distributions
● Linux, Unix, Mac OS – most likely already installed
● http://strawberryperl.com - Perl distribution for various
Windows
“When I am on Windows, I use Strawberry Perl” – Larry Wall
● http://www.activestate.com/activeperl/downloads - Perl
distributions for various platforms
● Q: Which version should I install?
● A: Any version 5.8 or higher should work for this class
Online Documentation
● Main website for documentation is
http://perldoc.perl.org/
● ActivePerl installs html version of documentation
● If you install ActivePerl, perldoc command
interactively to get documentation on various Perl
functions
● C:>perldoc -f print
● C:>perldoc -f sqrt
Online Documentation
● Perl documentation is divided into sections
● Sections you might find useful as a beginner:
 perlrun: How to execute the Perl interpreter
 perldata: Perl data types
 perlop: Perl operators and precedence
 perlfunc: Perl built-in functions
 perlre: Perl regular expressions
 perlsub: Perl user-defined subroutines
 perlsyn: Perl syntax such as loops and conditionals
Running Perl Code
“Hello, Perl!” on Windows
● Create hello_perl.pl in your favorite editor
● Type in the following two lines into that file:
● Run it from command line (set the PATH variable to
point to perl.exe):
 C:Perlsrc>perl hello_world.pl
use warnings;
print “Hello, Perl!n”;
Notes on Perl's Syntax
●
Whitespace does not matter as in C/C++
●
Curly braces { } group code in blocks as in C/C++
●
Semicolons at the end of statements as in C/C++
●
Unlike in Python, indentation is not required but makes
code legible
“Hello, Perl!” on Linux/Unix
●
Call the Perl interpreter directly:
 /home/user$ perl hello_perl.pl
●
Run it as a executable script:
 Add #!/usr/bin/perl at the beginning of hello_perl.pl
 Make the file executable: chmod +x hello_perl.pl
 Run it: /home/user$ ./hello_perl.pl
-w Warnings Option
●
On Windows, Linux/Unix: >perl -w some_program.pl
●
On Linux/Unix: place #!/usr/bin/perl -w at the
beginning of your program
●
On Windows, Linux/Unix: place use warnings; at the
beginning of your program
-w Warnings Option
●
If you run warnings_example.pl, you notice that the
Perl interpreter keeps on chugging even if the value of
the $x variable is not assigned
●
The recommended method is to place use warnings;
at the beginning of your file
●
Place use warnings; at the beginning of every Perl file
you submit as homework
Parentheses and Functions
●
Using parentheses with function calls is optional
print "Hellon";
print ("Hellon");
●
If code looks like a function, it is treated as a function
print 1 * 2 + 5; ## prints 7
print (1 * 2 + 5); ## prints 7
Adding Two Numbers
●
Source code: add_numbers.pl
●
$num1 - $ is a type identifier that states that the variable
$num1 is a scalar variable, i.e., holds a scalar value
●
The statement $num1 = <STDIN>; reads user input from
<STDIN> and places it into $num1
●
chomp $num1 removes 'n' from the end of the string
●
$sum = $num1 + $num2; converts the values in $num1
and $num2 to numbers, adds them and places the value into
$sum
Code Blocks
●
We can use {…} to group statements in a block
●
Blocks can be placed inside other blocks
●
Levels of nesting can be arbitrarily deep
●
Indentation can be used to increase legibility
●
Example: blocks.pl
Numbers and Strings
Basic Data Types
●
Perl has four built-in data types: literals, scalars,
arrays, and hashes (dictionaries)
●
A literal is a value that never changes
●
In Perl, there are three types of literals: integers,
floats, and strings
●
A scalar is a single value that can be either a number
or a string
Numbers
●
Integers:
0
1972
-10
●
Underscores can be used as commas to increase
legibility:
122038493
122_038_493
Numbers
●
Octals start with 0:
01 0523
●
Hexadecimals start with 0x:
0xFF 0x17abcdef
●
Binary numbers start with 0b:
0b11111 0b00001
●
Underscores are still allowed for readability:
0xFF45_FF12 0b1111_0001_1111
Numbers
●
Decimals:
1.234 .5
●
E notation:
1e2 -5e10 2e-5 1.75e3
●
Code sample: numbers.pl
Strings
●
In Perl, string processing is called interpolation
●
Single quoted strings are not interpolated with the
exception of  and 
●
Single quoted strings are treated as ordinary text
●
Double quoted strings are interpolated
●
In double quoted strings, variables are replaced with
their values and escape sequences are processed
Strings
use warnings;
print 'tHellonworldn!';
print "tHellonworldn!";
Strings
use warnings;
## this is useful on Windows
## escaping , aka backwhacking
print "n";
print "C:PerlCS3430n";
print 'C:PerlCS3430 ', "n";
q and qq
●
q/some text/ treats some text as a single quoted
string
●
Delimeters after q do not have to be /, they can be {},
[], <>, or ||
print q/C:Perlbinperl.exe/, "n";
print q[/usr/bin/perl.exe], "n";
print q{/usr/bin/perl.exe}, "n";
q and qq
●
qq/some text/ treats some text as a double quoted
string
●
Delimeters after qq do not have to be /, they can be {},
[], <>, or ||
print qq{tHe said, "Hello, world!"}, "n";
print qq[t/usr/bin/perl.exe], "n";
print qq|t'"Hi," said Jack. "Have you read a book today?"'n|;
Here-Documents
●
A here-document allows you to write large amounts of
text in your program and treat them as strings
●
A here-document starts with << immediately followed by
a start label, then some text, and a terminating label that
must be the same as the start label
●
The terminating label must be on a separate line by
itself with no semicolon
Here-Documents
print <<EOT;
This is a bunch of text.
The quick brown fox ...
Good bye.
EOT
Arithmetic Operators
Addition $x + $y
Subtraction $x - $y
Multiplication $x * $y
Division $x / $y
Modulus $x % $y
Exponentiation $x ** $y
Operator Precedence
●
Operators in expressions with parentheses are evaluated
first
●
Exponentiation operators are applied; if there are multiple
such operators, they are applied left to right
●
Multiplication, division operators are applied; if there are
multiple such operators, they are applied left to right
●
Addition and subtraction operators applied; if there are
multiple such operators, they are applied left to right
Example
Write a Perl program that evaluates the polynomial y
= a*x^2 + b*x + c at the user-supplied values of a, x,
b, c.
Solution in operator_precedence.pl
Assignment Operators
● Perl provides the C-style assignment operators for
abbreviating arithmetic operations in assignment
expressions
● Any statement of the form
variable = variable operator expression;
where operator is one of the binary operators (+, -, *,
/) can be written in the form
variable operator= expression;
Assignment Operators
$c = $c + 5; $c += 5;
$c = $c – 5; $c -= 5;
$c = $c * 5; $c *= 5;
$c = $c / 5; $c /= 5;
$c = $c % 5; $c %= 5;
$c = $c ** 5; $c **= 5;
Increment and Decrement Operators
++$c; increment $c by 1 and use the new value
of $c in the expression where $c
resides
$c++; use the current value of $c in the
expression in which $c resides, then
increment $c by 1
--$c; Same as ++$c except $c is decremented
by 1
$c--; same as $c++ except $c is decremented
by 1
Equality and Relational Operators
> $x > $y
< $x < $y
>= $x >= $y
<= $x <= $y
== $x == $y
!= $x != $y
String Operators
●
Perl provides a collection of comparison operators on
string scalars
●
String operators are: eq (“equals”), ne (“not equals”), lt
(“less than”), gt (“greater than”), le (“less than or
equal”), ge (“greater than or equal”)
●
Strings are compared alphabetically, with the letters
later in the alphabet having “greater” value
●
“rabbit” gt “dragon” is true, because the ASCII
value of 'r' (114) > the ASCII value of 'd' (100)
string_comp.pl
Example
String Concatenation and Repetition
●
The dot (.) is the string concatenation operator
print “a” . “b” . “c” . “n”;
●
A string s followed by the string repetition operator (x)
followed by an integer n concatenates n copies of s
together
print “Yeah” . “!” x 3 . “n”;
Scalar Contexts
Numeric and String Contexts
●
Scalar variables can refer to strings and numbers,
depending on where they occur in the program
●
Perl converts the value to a string or a number
depending on the context in which the scalar variable is
used
●
Uninitialized or undefined variables have the special
value undef which evaluates differently in different
contexts: in a numeric context, undef evaluates to 0; in
a string context, it evaluates to “”
scalar_context.pl
Example
Control Structures
if/else & if/elseif/else Selection
Let us implement this pseudocode:
If salesperson's sales >= 100
Print “$1500 bonus!”
Else
If salesperson's sales >= 50
Print “$200 bonus!”
Else
Print “You did not earn your bonus.”
if_unless.pl
if_else_if.pl
Example
do/while and do/until Loops
●
In addition to the standard while and until loops, Perl
has do/while and do/until loops
●
while and until loops check their condition first and then
execute the body of the loop
●
do/while and do/until check their condition after they
execute the body of the loop
do/while and do/until Loops
do {
statements;
} while ( condition );
do {
statements;
} until ( condition );
References
●
www.perl.org
●
James Lee. Beginning Perl, 2nd
Edition, APRESS
●
Dietel, Dietel, Nieto, McPhie. Perl How to Program,
Prentice Hall

Weitere ähnliche Inhalte

Was ist angesagt? (20)

Perl
PerlPerl
Perl
 
Php intro by sami kz
Php intro by sami kzPhp intro by sami kz
Php intro by sami kz
 
Php basics
Php basicsPhp basics
Php basics
 
php basics
php basicsphp basics
php basics
 
Introduction to php basics
Introduction to php   basicsIntroduction to php   basics
Introduction to php basics
 
Perl tutorial
Perl tutorialPerl tutorial
Perl tutorial
 
Andy On Closures
Andy On ClosuresAndy On Closures
Andy On Closures
 
PHP Basics
PHP BasicsPHP Basics
PHP Basics
 
Perl bhargav
Perl bhargavPerl bhargav
Perl bhargav
 
Theperlreview
TheperlreviewTheperlreview
Theperlreview
 
Lexical analyzer generator lex
Lexical analyzer generator lexLexical analyzer generator lex
Lexical analyzer generator lex
 
PHP Web Programming
PHP Web ProgrammingPHP Web Programming
PHP Web Programming
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
Lesson 3 php numbers
Lesson 3  php numbersLesson 3  php numbers
Lesson 3 php numbers
 
Dart workshop
Dart workshopDart workshop
Dart workshop
 
Dev traning 2016 basics of PHP
Dev traning 2016   basics of PHPDev traning 2016   basics of PHP
Dev traning 2016 basics of PHP
 
PHP
 PHP PHP
PHP
 
Java 8 by example!
Java 8 by example!Java 8 by example!
Java 8 by example!
 
PHP Course (Basic to Advance)
PHP Course (Basic to Advance)PHP Course (Basic to Advance)
PHP Course (Basic to Advance)
 
P H P Part I, By Kian
P H P  Part  I,  By  KianP H P  Part  I,  By  Kian
P H P Part I, By Kian
 

Andere mochten auch

Jordplan faktaark
Jordplan faktaarkJordplan faktaark
Jordplan faktaarkjordplan
 
Jorda på jordet 151105
Jorda på jordet 151105Jorda på jordet 151105
Jorda på jordet 151105jordplan
 
Tov 120314 tjenester fra skog og landskap
Tov 120314 tjenester fra skog og landskapTov 120314 tjenester fra skog og landskap
Tov 120314 tjenester fra skog og landskapjordplan
 
Jonsberg 141107 jordplan_final
Jonsberg 141107 jordplan_finalJonsberg 141107 jordplan_final
Jonsberg 141107 jordplan_finaljordplan
 
Jordplan faktaark a4
Jordplan faktaark a4Jordplan faktaark a4
Jordplan faktaark a4jordplan
 
Jordplan field summary_2015-11-19
Jordplan field summary_2015-11-19Jordplan field summary_2015-11-19
Jordplan field summary_2015-11-19jordplan
 
Jordplan drenering nmbu_sevu_140507
Jordplan drenering nmbu_sevu_140507Jordplan drenering nmbu_sevu_140507
Jordplan drenering nmbu_sevu_140507jordplan
 

Andere mochten auch (19)

Cs3430 lecture 14
Cs3430 lecture 14Cs3430 lecture 14
Cs3430 lecture 14
 
Jordplan faktaark
Jordplan faktaarkJordplan faktaark
Jordplan faktaark
 
Cs3430 lecture 17
Cs3430 lecture 17Cs3430 lecture 17
Cs3430 lecture 17
 
Jorda på jordet 151105
Jorda på jordet 151105Jorda på jordet 151105
Jorda på jordet 151105
 
Python lecture 10
Python lecture 10Python lecture 10
Python lecture 10
 
Python lecture 04
Python lecture 04Python lecture 04
Python lecture 04
 
Python lecture 02
Python lecture 02Python lecture 02
Python lecture 02
 
Tov 120314 tjenester fra skog og landskap
Tov 120314 tjenester fra skog og landskapTov 120314 tjenester fra skog og landskap
Tov 120314 tjenester fra skog og landskap
 
Jonsberg 141107 jordplan_final
Jonsberg 141107 jordplan_finalJonsberg 141107 jordplan_final
Jonsberg 141107 jordplan_final
 
Jordplan faktaark a4
Jordplan faktaark a4Jordplan faktaark a4
Jordplan faktaark a4
 
Python lecture 03
Python lecture 03Python lecture 03
Python lecture 03
 
Cs3430 lecture 13
Cs3430 lecture 13Cs3430 lecture 13
Cs3430 lecture 13
 
Python lecture 8
Python lecture 8Python lecture 8
Python lecture 8
 
Jordplan field summary_2015-11-19
Jordplan field summary_2015-11-19Jordplan field summary_2015-11-19
Jordplan field summary_2015-11-19
 
Jordplan drenering nmbu_sevu_140507
Jordplan drenering nmbu_sevu_140507Jordplan drenering nmbu_sevu_140507
Jordplan drenering nmbu_sevu_140507
 
Python lecture 07
Python lecture 07Python lecture 07
Python lecture 07
 
Python lecture 09
Python lecture 09Python lecture 09
Python lecture 09
 
Python lecture 06
Python lecture 06Python lecture 06
Python lecture 06
 
Python lecture 12
Python lecture 12Python lecture 12
Python lecture 12
 

Ähnlich wie Cs3430 lecture 15

Ähnlich wie Cs3430 lecture 15 (20)

Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 
Perl Basics with Examples
Perl Basics with ExamplesPerl Basics with Examples
Perl Basics with Examples
 
Perl slid
Perl slidPerl slid
Perl slid
 
Bioinformatics p1-perl-introduction v2013
Bioinformatics p1-perl-introduction v2013Bioinformatics p1-perl-introduction v2013
Bioinformatics p1-perl-introduction v2013
 
Perl - laziness, impatience, hubris, and one liners
Perl - laziness, impatience, hubris, and one linersPerl - laziness, impatience, hubris, and one liners
Perl - laziness, impatience, hubris, and one liners
 
Basics of PHP by Dr.C.R.Dhivyaa Kongu Engineering College
Basics of PHP by Dr.C.R.Dhivyaa Kongu Engineering CollegeBasics of PHP by Dr.C.R.Dhivyaa Kongu Engineering College
Basics of PHP by Dr.C.R.Dhivyaa Kongu Engineering College
 
UNIT 1 (7).pptx
UNIT 1 (7).pptxUNIT 1 (7).pptx
UNIT 1 (7).pptx
 
UNIT 1 (7).pptx
UNIT 1 (7).pptxUNIT 1 (7).pptx
UNIT 1 (7).pptx
 
Perl5 Memory Manglement
Perl5 Memory ManglementPerl5 Memory Manglement
Perl5 Memory Manglement
 
A brief introduction to C Language
A brief introduction to C LanguageA brief introduction to C Language
A brief introduction to C Language
 
perltut
perltutperltut
perltut
 
perltut
perltutperltut
perltut
 
python and perl
python and perlpython and perl
python and perl
 
Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++
 
Intro to Perl
Intro to PerlIntro to Perl
Intro to Perl
 
Bioinformatica 29-09-2011-p1-introduction
Bioinformatica 29-09-2011-p1-introductionBioinformatica 29-09-2011-p1-introduction
Bioinformatica 29-09-2011-p1-introduction
 
Perl_Part1
Perl_Part1Perl_Part1
Perl_Part1
 
Functional programming in Scala
Functional programming in ScalaFunctional programming in Scala
Functional programming in Scala
 
shellScriptAlt.pptx
shellScriptAlt.pptxshellScriptAlt.pptx
shellScriptAlt.pptx
 
Getting started with Perl XS and Inline::C
Getting started with Perl XS and Inline::CGetting started with Perl XS and Inline::C
Getting started with Perl XS and Inline::C
 

Kürzlich hochgeladen

CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...Nguyen Thanh Tu Collection
 
Diploma in Nursing Admission Test Question Solution 2023.pdf
Diploma in Nursing Admission Test Question Solution 2023.pdfDiploma in Nursing Admission Test Question Solution 2023.pdf
Diploma in Nursing Admission Test Question Solution 2023.pdfMohonDas
 
The basics of sentences session 10pptx.pptx
The basics of sentences session 10pptx.pptxThe basics of sentences session 10pptx.pptx
The basics of sentences session 10pptx.pptxheathfieldcps1
 
Philosophy of Education and Educational Philosophy
Philosophy of Education  and Educational PhilosophyPhilosophy of Education  and Educational Philosophy
Philosophy of Education and Educational PhilosophyShuvankar Madhu
 
How to Solve Singleton Error in the Odoo 17
How to Solve Singleton Error in the  Odoo 17How to Solve Singleton Error in the  Odoo 17
How to Solve Singleton Error in the Odoo 17Celine George
 
How to Manage Cross-Selling in Odoo 17 Sales
How to Manage Cross-Selling in Odoo 17 SalesHow to Manage Cross-Selling in Odoo 17 Sales
How to Manage Cross-Selling in Odoo 17 SalesCeline George
 
General views of Histopathology and step
General views of Histopathology and stepGeneral views of Histopathology and step
General views of Histopathology and stepobaje godwin sunday
 
UKCGE Parental Leave Discussion March 2024
UKCGE Parental Leave Discussion March 2024UKCGE Parental Leave Discussion March 2024
UKCGE Parental Leave Discussion March 2024UKCGE
 
How to Make a Field read-only in Odoo 17
How to Make a Field read-only in Odoo 17How to Make a Field read-only in Odoo 17
How to Make a Field read-only in Odoo 17Celine George
 
AUDIENCE THEORY -- FANDOM -- JENKINS.pptx
AUDIENCE THEORY -- FANDOM -- JENKINS.pptxAUDIENCE THEORY -- FANDOM -- JENKINS.pptx
AUDIENCE THEORY -- FANDOM -- JENKINS.pptxiammrhaywood
 
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptxSandy Millin
 
3.21.24 The Origins of Black Power.pptx
3.21.24  The Origins of Black Power.pptx3.21.24  The Origins of Black Power.pptx
3.21.24 The Origins of Black Power.pptxmary850239
 
How to Add a New Field in Existing Kanban View in Odoo 17
How to Add a New Field in Existing Kanban View in Odoo 17How to Add a New Field in Existing Kanban View in Odoo 17
How to Add a New Field in Existing Kanban View in Odoo 17Celine George
 
HED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdfHED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdfMohonDas
 
Education and training program in the hospital APR.pptx
Education and training program in the hospital APR.pptxEducation and training program in the hospital APR.pptx
Education and training program in the hospital APR.pptxraviapr7
 
Clinical Pharmacy Introduction to Clinical Pharmacy, Concept of clinical pptx
Clinical Pharmacy  Introduction to Clinical Pharmacy, Concept of clinical pptxClinical Pharmacy  Introduction to Clinical Pharmacy, Concept of clinical pptx
Clinical Pharmacy Introduction to Clinical Pharmacy, Concept of clinical pptxraviapr7
 
CapTechU Doctoral Presentation -March 2024 slides.pptx
CapTechU Doctoral Presentation -March 2024 slides.pptxCapTechU Doctoral Presentation -March 2024 slides.pptx
CapTechU Doctoral Presentation -March 2024 slides.pptxCapitolTechU
 
Human-AI Co-Creation of Worked Examples for Programming Classes
Human-AI Co-Creation of Worked Examples for Programming ClassesHuman-AI Co-Creation of Worked Examples for Programming Classes
Human-AI Co-Creation of Worked Examples for Programming ClassesMohammad Hassany
 
Ultra structure and life cycle of Plasmodium.pptx
Ultra structure and life cycle of Plasmodium.pptxUltra structure and life cycle of Plasmodium.pptx
Ultra structure and life cycle of Plasmodium.pptxDr. Asif Anas
 
The Stolen Bacillus by Herbert George Wells
The Stolen Bacillus by Herbert George WellsThe Stolen Bacillus by Herbert George Wells
The Stolen Bacillus by Herbert George WellsEugene Lysak
 

Kürzlich hochgeladen (20)

CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
CHUYÊN ĐỀ DẠY THÊM TIẾNG ANH LỚP 11 - GLOBAL SUCCESS - NĂM HỌC 2023-2024 - HK...
 
Diploma in Nursing Admission Test Question Solution 2023.pdf
Diploma in Nursing Admission Test Question Solution 2023.pdfDiploma in Nursing Admission Test Question Solution 2023.pdf
Diploma in Nursing Admission Test Question Solution 2023.pdf
 
The basics of sentences session 10pptx.pptx
The basics of sentences session 10pptx.pptxThe basics of sentences session 10pptx.pptx
The basics of sentences session 10pptx.pptx
 
Philosophy of Education and Educational Philosophy
Philosophy of Education  and Educational PhilosophyPhilosophy of Education  and Educational Philosophy
Philosophy of Education and Educational Philosophy
 
How to Solve Singleton Error in the Odoo 17
How to Solve Singleton Error in the  Odoo 17How to Solve Singleton Error in the  Odoo 17
How to Solve Singleton Error in the Odoo 17
 
How to Manage Cross-Selling in Odoo 17 Sales
How to Manage Cross-Selling in Odoo 17 SalesHow to Manage Cross-Selling in Odoo 17 Sales
How to Manage Cross-Selling in Odoo 17 Sales
 
General views of Histopathology and step
General views of Histopathology and stepGeneral views of Histopathology and step
General views of Histopathology and step
 
UKCGE Parental Leave Discussion March 2024
UKCGE Parental Leave Discussion March 2024UKCGE Parental Leave Discussion March 2024
UKCGE Parental Leave Discussion March 2024
 
How to Make a Field read-only in Odoo 17
How to Make a Field read-only in Odoo 17How to Make a Field read-only in Odoo 17
How to Make a Field read-only in Odoo 17
 
AUDIENCE THEORY -- FANDOM -- JENKINS.pptx
AUDIENCE THEORY -- FANDOM -- JENKINS.pptxAUDIENCE THEORY -- FANDOM -- JENKINS.pptx
AUDIENCE THEORY -- FANDOM -- JENKINS.pptx
 
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx
 
3.21.24 The Origins of Black Power.pptx
3.21.24  The Origins of Black Power.pptx3.21.24  The Origins of Black Power.pptx
3.21.24 The Origins of Black Power.pptx
 
How to Add a New Field in Existing Kanban View in Odoo 17
How to Add a New Field in Existing Kanban View in Odoo 17How to Add a New Field in Existing Kanban View in Odoo 17
How to Add a New Field in Existing Kanban View in Odoo 17
 
HED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdfHED Office Sohayok Exam Question Solution 2023.pdf
HED Office Sohayok Exam Question Solution 2023.pdf
 
Education and training program in the hospital APR.pptx
Education and training program in the hospital APR.pptxEducation and training program in the hospital APR.pptx
Education and training program in the hospital APR.pptx
 
Clinical Pharmacy Introduction to Clinical Pharmacy, Concept of clinical pptx
Clinical Pharmacy  Introduction to Clinical Pharmacy, Concept of clinical pptxClinical Pharmacy  Introduction to Clinical Pharmacy, Concept of clinical pptx
Clinical Pharmacy Introduction to Clinical Pharmacy, Concept of clinical pptx
 
CapTechU Doctoral Presentation -March 2024 slides.pptx
CapTechU Doctoral Presentation -March 2024 slides.pptxCapTechU Doctoral Presentation -March 2024 slides.pptx
CapTechU Doctoral Presentation -March 2024 slides.pptx
 
Human-AI Co-Creation of Worked Examples for Programming Classes
Human-AI Co-Creation of Worked Examples for Programming ClassesHuman-AI Co-Creation of Worked Examples for Programming Classes
Human-AI Co-Creation of Worked Examples for Programming Classes
 
Ultra structure and life cycle of Plasmodium.pptx
Ultra structure and life cycle of Plasmodium.pptxUltra structure and life cycle of Plasmodium.pptx
Ultra structure and life cycle of Plasmodium.pptx
 
The Stolen Bacillus by Herbert George Wells
The Stolen Bacillus by Herbert George WellsThe Stolen Bacillus by Herbert George Wells
The Stolen Bacillus by Herbert George Wells
 

Cs3430 lecture 15

  • 1. CS 3430: Introduction to Python and Perl Lecture 15 Department of Computer Science Utah State University
  • 2. Overview ● Introduction To Perl ● Installation, Editing, Documentation ● Running Perl programs ● Numbers and strings ● Control Structures
  • 4. Learning Objectives ● Familiarity, not competence ● Online resources ● Basic coding principles
  • 5. History ● Larry Wall began to develop Perl in 1987 ● The language was originally named Pearl, but there was a language by that name ● Perl stands for “Practical Extraction and Report Language” ● Rumor has it that the name Perl came first and the expansion came later
  • 6. Perl Principles ● There is more than one way to do it ● Perl: Swiss Army chainsaw of programming languages ● No unnecessary limits ● Perl is the duct tape of the Internet (not as true as it used to be anymore)
  • 7. What Perl is Used For ● Internet and Web Scripting ● Component Integration ● Database Programming ● Rapid Prototyping ● Data Mining
  • 9. Perl's Weaknesses ● Slower than C/C++ ● Long and rather steep learning curve ● Code can be difficult to read  Joke: Perl is a “write only” language  Hard even for the original programmer to read later
  • 10. Influences on Perl ● AWK ● C/C++ ● Lisp ● Pascal ● Unix shell ● BASIC-PLUS
  • 11. Resources ● http://www.perl.org - site for everything that is Perl – documentation, IDEs, tutorials, links to helpful resourcesa ● http://cpan.perl.org - Comprehensive Perl Archive Network – a lot of helpful Perl tools and third-party libraries
  • 13. Perl Distributions ● Linux, Unix, Mac OS – most likely already installed ● http://strawberryperl.com - Perl distribution for various Windows “When I am on Windows, I use Strawberry Perl” – Larry Wall ● http://www.activestate.com/activeperl/downloads - Perl distributions for various platforms ● Q: Which version should I install? ● A: Any version 5.8 or higher should work for this class
  • 14. Online Documentation ● Main website for documentation is http://perldoc.perl.org/ ● ActivePerl installs html version of documentation ● If you install ActivePerl, perldoc command interactively to get documentation on various Perl functions ● C:>perldoc -f print ● C:>perldoc -f sqrt
  • 15. Online Documentation ● Perl documentation is divided into sections ● Sections you might find useful as a beginner:  perlrun: How to execute the Perl interpreter  perldata: Perl data types  perlop: Perl operators and precedence  perlfunc: Perl built-in functions  perlre: Perl regular expressions  perlsub: Perl user-defined subroutines  perlsyn: Perl syntax such as loops and conditionals
  • 17. “Hello, Perl!” on Windows ● Create hello_perl.pl in your favorite editor ● Type in the following two lines into that file: ● Run it from command line (set the PATH variable to point to perl.exe):  C:Perlsrc>perl hello_world.pl use warnings; print “Hello, Perl!n”;
  • 18. Notes on Perl's Syntax ● Whitespace does not matter as in C/C++ ● Curly braces { } group code in blocks as in C/C++ ● Semicolons at the end of statements as in C/C++ ● Unlike in Python, indentation is not required but makes code legible
  • 19. “Hello, Perl!” on Linux/Unix ● Call the Perl interpreter directly:  /home/user$ perl hello_perl.pl ● Run it as a executable script:  Add #!/usr/bin/perl at the beginning of hello_perl.pl  Make the file executable: chmod +x hello_perl.pl  Run it: /home/user$ ./hello_perl.pl
  • 20. -w Warnings Option ● On Windows, Linux/Unix: >perl -w some_program.pl ● On Linux/Unix: place #!/usr/bin/perl -w at the beginning of your program ● On Windows, Linux/Unix: place use warnings; at the beginning of your program
  • 21. -w Warnings Option ● If you run warnings_example.pl, you notice that the Perl interpreter keeps on chugging even if the value of the $x variable is not assigned ● The recommended method is to place use warnings; at the beginning of your file ● Place use warnings; at the beginning of every Perl file you submit as homework
  • 22. Parentheses and Functions ● Using parentheses with function calls is optional print "Hellon"; print ("Hellon"); ● If code looks like a function, it is treated as a function print 1 * 2 + 5; ## prints 7 print (1 * 2 + 5); ## prints 7
  • 23. Adding Two Numbers ● Source code: add_numbers.pl ● $num1 - $ is a type identifier that states that the variable $num1 is a scalar variable, i.e., holds a scalar value ● The statement $num1 = <STDIN>; reads user input from <STDIN> and places it into $num1 ● chomp $num1 removes 'n' from the end of the string ● $sum = $num1 + $num2; converts the values in $num1 and $num2 to numbers, adds them and places the value into $sum
  • 24. Code Blocks ● We can use {…} to group statements in a block ● Blocks can be placed inside other blocks ● Levels of nesting can be arbitrarily deep ● Indentation can be used to increase legibility ● Example: blocks.pl
  • 26. Basic Data Types ● Perl has four built-in data types: literals, scalars, arrays, and hashes (dictionaries) ● A literal is a value that never changes ● In Perl, there are three types of literals: integers, floats, and strings ● A scalar is a single value that can be either a number or a string
  • 27. Numbers ● Integers: 0 1972 -10 ● Underscores can be used as commas to increase legibility: 122038493 122_038_493
  • 28. Numbers ● Octals start with 0: 01 0523 ● Hexadecimals start with 0x: 0xFF 0x17abcdef ● Binary numbers start with 0b: 0b11111 0b00001 ● Underscores are still allowed for readability: 0xFF45_FF12 0b1111_0001_1111
  • 29. Numbers ● Decimals: 1.234 .5 ● E notation: 1e2 -5e10 2e-5 1.75e3 ● Code sample: numbers.pl
  • 30. Strings ● In Perl, string processing is called interpolation ● Single quoted strings are not interpolated with the exception of and ● Single quoted strings are treated as ordinary text ● Double quoted strings are interpolated ● In double quoted strings, variables are replaced with their values and escape sequences are processed
  • 32. Strings use warnings; ## this is useful on Windows ## escaping , aka backwhacking print "n"; print "C:PerlCS3430n"; print 'C:PerlCS3430 ', "n";
  • 33. q and qq ● q/some text/ treats some text as a single quoted string ● Delimeters after q do not have to be /, they can be {}, [], <>, or || print q/C:Perlbinperl.exe/, "n"; print q[/usr/bin/perl.exe], "n"; print q{/usr/bin/perl.exe}, "n";
  • 34. q and qq ● qq/some text/ treats some text as a double quoted string ● Delimeters after qq do not have to be /, they can be {}, [], <>, or || print qq{tHe said, "Hello, world!"}, "n"; print qq[t/usr/bin/perl.exe], "n"; print qq|t'"Hi," said Jack. "Have you read a book today?"'n|;
  • 35. Here-Documents ● A here-document allows you to write large amounts of text in your program and treat them as strings ● A here-document starts with << immediately followed by a start label, then some text, and a terminating label that must be the same as the start label ● The terminating label must be on a separate line by itself with no semicolon
  • 36. Here-Documents print <<EOT; This is a bunch of text. The quick brown fox ... Good bye. EOT
  • 37. Arithmetic Operators Addition $x + $y Subtraction $x - $y Multiplication $x * $y Division $x / $y Modulus $x % $y Exponentiation $x ** $y
  • 38. Operator Precedence ● Operators in expressions with parentheses are evaluated first ● Exponentiation operators are applied; if there are multiple such operators, they are applied left to right ● Multiplication, division operators are applied; if there are multiple such operators, they are applied left to right ● Addition and subtraction operators applied; if there are multiple such operators, they are applied left to right
  • 39. Example Write a Perl program that evaluates the polynomial y = a*x^2 + b*x + c at the user-supplied values of a, x, b, c. Solution in operator_precedence.pl
  • 40. Assignment Operators ● Perl provides the C-style assignment operators for abbreviating arithmetic operations in assignment expressions ● Any statement of the form variable = variable operator expression; where operator is one of the binary operators (+, -, *, /) can be written in the form variable operator= expression;
  • 41. Assignment Operators $c = $c + 5; $c += 5; $c = $c – 5; $c -= 5; $c = $c * 5; $c *= 5; $c = $c / 5; $c /= 5; $c = $c % 5; $c %= 5; $c = $c ** 5; $c **= 5;
  • 42. Increment and Decrement Operators ++$c; increment $c by 1 and use the new value of $c in the expression where $c resides $c++; use the current value of $c in the expression in which $c resides, then increment $c by 1 --$c; Same as ++$c except $c is decremented by 1 $c--; same as $c++ except $c is decremented by 1
  • 43. Equality and Relational Operators > $x > $y < $x < $y >= $x >= $y <= $x <= $y == $x == $y != $x != $y
  • 44. String Operators ● Perl provides a collection of comparison operators on string scalars ● String operators are: eq (“equals”), ne (“not equals”), lt (“less than”), gt (“greater than”), le (“less than or equal”), ge (“greater than or equal”) ● Strings are compared alphabetically, with the letters later in the alphabet having “greater” value ● “rabbit” gt “dragon” is true, because the ASCII value of 'r' (114) > the ASCII value of 'd' (100)
  • 46. String Concatenation and Repetition ● The dot (.) is the string concatenation operator print “a” . “b” . “c” . “n”; ● A string s followed by the string repetition operator (x) followed by an integer n concatenates n copies of s together print “Yeah” . “!” x 3 . “n”;
  • 48. Numeric and String Contexts ● Scalar variables can refer to strings and numbers, depending on where they occur in the program ● Perl converts the value to a string or a number depending on the context in which the scalar variable is used ● Uninitialized or undefined variables have the special value undef which evaluates differently in different contexts: in a numeric context, undef evaluates to 0; in a string context, it evaluates to “”
  • 51. if/else & if/elseif/else Selection Let us implement this pseudocode: If salesperson's sales >= 100 Print “$1500 bonus!” Else If salesperson's sales >= 50 Print “$200 bonus!” Else Print “You did not earn your bonus.”
  • 53. do/while and do/until Loops ● In addition to the standard while and until loops, Perl has do/while and do/until loops ● while and until loops check their condition first and then execute the body of the loop ● do/while and do/until check their condition after they execute the body of the loop
  • 54. do/while and do/until Loops do { statements; } while ( condition ); do { statements; } until ( condition );
  • 55. References ● www.perl.org ● James Lee. Beginning Perl, 2nd Edition, APRESS ● Dietel, Dietel, Nieto, McPhie. Perl How to Program, Prentice Hall