82
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Embed Size (px)

Citation preview

Page 1: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Copyright © 2003 ProsoftTraining. All rights reserved.

PerlFundamentals

Page 2: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Copyright © 2003 ProsoftTraining. All rights reserved.

Lesson 1:Introduction to Perl

Page 3: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Objectives

• Describe the benefits of Perl• Explain the role of the Perl interpreter• Identify the characteristics of Perl’s basic

syntax• Describe the use of the print function• Create and execute a simple Perl script• Define scalar variables• Use scalar variables to manipulate numerical

and string data• Use expression operators• Retrieve data from STDIN

Page 4: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Practical Extractingand Reporting Language

• Why use Perl?– Innate flexibility– Simple syntax– Relaxed compiler instructions– Free

Page 5: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Getting Startedwith Perl

• The shebang line• Creating a simple Perl script

Page 6: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Scalar andNumerical Variables

• Assignment• Expressions

Page 7: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

String Variables

• Second type of scalar variable• The print function

Page 8: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

RetrievingData from STDIN

• The chomp() function

Page 9: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Summary

Describe the benefits of Perl Explain the role of the Perl interpreter Identify the characteristics of Perl’s basic

syntax Describe the use of the print function Create and execute a simple Perl script Define scalar variables Use scalar variables to manipulate numerical

and string data Use expression operators Retrieve data from STDIN

Page 10: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Copyright © 2003 ProsoftTraining. All rights reserved.

Lesson 2:Flow Control

in Perl

Page 11: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Objectives

• Evaluate Boolean expressions• Construct an if statement• Discuss else and elsif branches• Construct a while loop, a do {} while loop,

and a for loop• Use loop-control commands• Describe the I/O redirection paradigm

Page 12: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

BooleanExpressions in Perl

• Numeric Boolean expressions• String Boolean expressions• Logical operators

Page 13: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

The ifStatement

• The else branch• The elsif branch

Page 14: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

The whileStatement

• Second type of control structure• Defines a block of code that will be executed

repeatedly as long as some Boolean expression evaluates as true

Page 15: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

The do { } whileStatement

• Similar to the while loop except that the condition is not evaluated until the code block has already been executed once

Page 16: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

The forStatement

• Includes three expressions separated by semicolons

• Incorporates facilities for initializing a counter and incrementing it on each turn through the code block

Page 17: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Loop-ControlCommands

• last• next• redo

Page 18: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

I/O Redirection

• Many Perl scripts use I/O redirection in place of more complicated file-handling subroutines

Page 19: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Summary

Evaluate Boolean expressions Construct an if statement Discuss else and elsif branches Construct a while loop, a do {} while loop,

and a for loop Use loop-control commands Describe the I/O redirection paradigm

Page 20: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Copyright © 2003 ProsoftTraining. All rights reserved.

Lesson 3:Regular

Expressions in Perl

Page 21: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Objectives

• Define regular expressions• Perform pattern matching• Define and use metacharacters, quantifiers

and assertions• Explain character classes• Perform substitution• Use the binding operator

Page 22: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Introduction toRegular Expressions

• Pattern binding operators• Escape sequences and metacharacters

Page 23: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Character Classes

• Indicate a list of characters that one element in a string will match

Page 24: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Pattern Matchingand Substitution

• Back references

Page 25: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Summary

Define regular expressions Perform pattern matching Define and use metacharacters, quantifiers

and assertions Explain character classes Perform substitution Use the binding operator

Page 26: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Copyright © 2003 ProsoftTraining. All rights reserved.

Lesson 4:Arrays in Perl

Page 27: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Objectives

• Describe the purpose of arrays• Define arrays using lists• Access array elements• Use the sort function to sort an array

alphabetically• Use a foreach loop to traverse an array• Use the push, pop, shift, unshift, split and join functions

Page 28: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Introductionto Perl Arrays

• Initializing arrays• Accessing array elements

Page 29: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

The sortFunction

• Accepts an array as an argument, alphabetizes the elements within the array, and returns the resultant array

Page 30: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

The foreachStatement

• A special control structure designed to iterate through an array or list

Page 31: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

The push andpop Functions

• The push function adds values to the top of a stack

• The pop function removes values from a stack

Page 32: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

The shift andunshift Functions

• The unshift function adds a value to the front of an array and shifts the rest of the array by one

• The shift function removes values from an array

• Using an array as a queue

Page 33: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

The split andjoin Functions

• The split function accepts two arguments, a regular expression and a string

• The join function accepts a list of values and combines them into a single string

Page 34: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Summary

Describe the purpose of arrays Define arrays using lists Access array elements Use the sort function to sort an array

alphabetically Use a foreach loop to traverse an array Use the push, pop, shift, unshift, split and join functions

Page 35: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Copyright © 2003 ProsoftTraining. All rights reserved.

Lesson 5:Hashes in Perl

Page 36: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Objectives

• Describe the purpose of hashes• Define hashes using lists• Access hash elements• Use the delete, keys, values, each, and reverse functions

Page 37: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Introductionto Perl Hashes

• Hashes are collections of scalar values that can be accessed individually

• Hash elements are accessed using an arbitrary scalar value, called a key

• Also known as associative arrays

Page 38: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Adding andDeleting Hash Elements

• The delete function• The keys function• The values function• The each function• The reverse function

Page 39: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Summary

Describe the purpose of hashes Define hashes using lists Access hash elements Use the delete, keys, values, each, and reverse functions

Page 40: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Copyright © 2003 ProsoftTraining. All rights reserved.

Lesson 6:Subroutines

in Perl

Page 41: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Objectives

• Define and use a subroutine• Call subroutines directly and indirectly• Pass values to a subroutine• Pass references to a subroutine• Explain variable scope• Return a value from a subroutine

Page 42: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Introductionto Perl Subroutines

• Defining subroutines• Calling subroutines• Passing arguments• Returning values• The sort function and subroutines

Page 43: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Variable Scope

• Variables can be created within subroutines that are private (specific) to just that subroutine using the my operator– The my operator takes a scalar, array, or

hash name and instantiates local versions inside a subroutine

Page 44: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

References

• Creating and referencing anonymous arrays• Creating and referencing anonymous hashes• Passing references to subroutines

Page 45: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Summary

Define and use a subroutine Call subroutines directly and indirectly Pass values to a subroutine Pass references to a subroutine Explain variable scope Return a value from a subroutine

Page 46: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Copyright © 2003 ProsoftTraining. All rights reserved.

Lesson 7:File Input

and Output

Page 47: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Objectives

• Define and use filehandles• Obtain a filehandle using the open function• Output data to a file• Close a file using the close function• Open a file for reading• Use the stat and lstat functions to obtain

information about a file

Page 48: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Perl File Input and Output

• What is a filehandle?• The open function• Outputting data to a file• Opening files for reading• Other file-related functions• Determining information about files• The stat and lstat functions

Page 49: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Summary

Define and use filehandles Obtain a filehandle using the open function Output data to a file Close a file using the close function Open a file for reading Use the stat and lstat functions to obtain

information about a file

Page 50: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Copyright © 2003 ProsoftTraining. All rights reserved.

Lesson 8:Environment Variables

and Command Line Arguments

Page 51: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Objectives

• Access and use environment variables• Use command line arguments• Define options when handling command line

arguments

Page 52: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Environment Variables

• What are environment variables?– Shells

Page 53: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Command LineArguments

• Arguments entered at the command line can be used in Perl programs

Page 54: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Summary

Access and use environment variables Use command line arguments Define options when handling command line

arguments

Page 55: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Copyright © 2003 ProsoftTraining. All rights reserved.

Lesson 9:Packages and Modules in Perl

Page 56: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Objectives

• Describe the purpose of packages• Use the package keyword• Use BEGIN and END blocks• Describe the purpose of modules• Create a module to facilitate code reuse• Incorporate a module into your Perl scripts

using the use and require statements• Use the Exporter module

Page 57: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Using Packages in Perl

• Namespace• The package keyword• Package symbol tables

Page 58: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

BEGIN and END Blocks

• Special blocks of code defined within a package

Page 59: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

UsingModules in Perl

• Specially designed Perl scripts that package functionality for reuse by other Perl scripts

Page 60: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

The use andrequire Statements

• The require statement takes a single argument (the name of the module to include)

• The use statement adds symbols directly to the including package’s symbol table

Page 61: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Summary

Describe the purpose of packages Use the package keyword Use BEGIN and END blocks Describe the purpose of modules Create a module to facilitate code reuse Incorporate a module into your Perl scripts

using the use and require statements Use the Exporter module

Page 62: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Copyright © 2003 ProsoftTraining. All rights reserved.

Lesson 10:Object-Oriented Perl

Page 63: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Objectives

• Describe the purpose of objects• Define objects for use in your Perl scripts• Access object data• Define and use object methods• Use inheritance to expand the functionality of

a class

Page 64: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Introduction toObject-Oriented Perl

• Creating objects• Object data• Object methods

Page 65: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Inheritance

• @ISA array• Destructor methods

Page 66: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Summary

Describe the purpose of objects Define objects for use in your Perl scripts Access object data Define and use object methods Use inheritance to expand the functionality of

a class

Page 67: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Copyright © 2003 ProsoftTraining. All rights reserved.

Lesson 11:Database

Connectivity and Perl

Page 68: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Objectives

• Define database programming• Explain the benefits of using a database• Define and use the DBI, the DBD, and SQL• Open a database connection• Query a database• Return records from a database• Insert records into a database• Close a connection to a database

Page 69: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Introduction toDatabase Connectivity

• Database programming• Database Interface Module• Database Driver Module

Page 70: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Interactingwith Databases

1. Connect to the database

2. Query the database

3. Display the results

4. Close the connection

Page 71: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Connectingto Databases

• The connect method

Page 72: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

StructuredQuery Language

• Data Definition Language– CREATE– DROP

• Data Query Language• Data Manipulation Language

– INSERT– DELETE– UPDATE

Page 73: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

QuotingOperators

• Perl includes quoting operators that can be used instead of single or double quotation marks

Page 74: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Summary

Define database programming Explain the benefits of using a database Define and use the DBI, the DBD, and SQL Open a database connection Query a database Return records from a database Insert records into a database Close a connection to a database

Page 75: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Copyright © 2003 ProsoftTraining. All rights reserved.

Lesson 12:Debugging

Perl Programs

Page 76: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Objectives

• Debug Perl programs• Use the –w switch• Use the strict module• Issue commands to the Perl debugger• Trace the execution of a Perl script• Design Perl scripts to minimize bugs

Page 77: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Introduction toDebugging Perl Scripts

• Using the print command• Using the –w switch• Using the strict module

Page 78: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

The PerlDebugger

• Traps and fixes errors in a Perl script• An interactive Perl environment wherein the

user is prompted for debugger commands

Page 79: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

WritingBug-Free Perl Code

• Preventing errors• Common Perl errors

Page 80: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Summary

Debug Perl programs Use the –w switch Use the strict module Issue commands to the Perl debugger Trace the execution of a Perl script Design Perl scripts to minimize bugs

Page 81: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

Perl Fundamentals

Introduction to Perl Flow Control in Perl Regular Expressions in Perl Arrays in Perl Hashes in Perl Subroutines in Perl File Input and Output in Perl

Page 82: Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals

PerlFundamentals

Environment Variables and Command Line Arguments

Packages and Modules in Perl Object-Oriented Perl Database Connectivity and Perl Debugging Perl Programs