1. Trang chủ
  2. » Công Nghệ Thông Tin

Beginning perl programming self study guide student manual 2018 19 edition

344 38 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 344
Dung lượng 2,42 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Table of Contents ©2018 One Course Source, Inc... Table of Contents ©2018 One Course Source, Inc... Page 9 Introduction About this course This manual was designed with the goal of ass

Trang 1

Beginning Perl Programming

Self-Study Guide &

Student Manual

2018-19 Edition

William "Bo" Rothwell

A publication by

Trang 2

Copyright One Course Source, 2018 ALL RIGHTS RESERVED

This publication contains proprietary and confidential information, which is the property of One Course Source, Inc No part of this publication is be reproduced, copied, disclosed, transmitted, stored in a retrieval system or translated into any human or computer language, in any form, by any means, in whole or in part, without the prior express written consent of One Course Source

THIS PUBLICATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED,

INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT

REFERENCES TO CORPORATIONS, THEIR SERVICES AND PRODUCTS, ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED IN NO EVENT SHALL ONE COURSE SOURCE BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE POSSIBILITY

OF DAMAGE, AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ARISING OUT

OF OR IN CONNECTION WITH THE USE OF THIS INFORMATION

Descriptions of, or references to, products or publications within this publication do not imply endorsement of that product or publication One Course Source makes no warranty of any kind with respect to the subject matter included herein, the products listed herein, or the completeness or accuracy of this publication One Course Source specifically disclaims all warranties, express, implied or otherwise, including without limitation, all warranties of merchantability and fitness for a particular purpose

THIS PUBLICATION COULD INCLUDE TECHNICAL INACCURACIES OR TYPOGRAPHICAL ERRORS CHANGES ARE PERIODICALLY ADDED TO THE INFORMATION HEREIN; THESE CHANGES WILL BE INCORPORATED IN NEW EDITIONS

OF THE PUBLICATION ONE COURSE SOURCE MAY MAKE IMPROVEMENTS AND/OR CHANGES IN THE PRODUCT(S) AND/OR THE PROGRAM(S) DESCRIBED IN THIS PUBLICATION AT ANY TIME

This notice may not be removed or altered

Trang 3

Table of Contents ©2018 One Course Source, Inc Page 3

2.1 Numeric Literals 39 2.2 Manipulating Numbers 40 2.3 String Literals 43 2.4 Manipulating Strings 44 2.5 Strings vs Numbers 48 2.6 Single vs Double Quotes 52 2.7 Scalars Variables 56 2.8 Undefined variables 57 2.9 Auto increment and Auto decrement 61 2.10 Reading data from the user 66 2.11 chomp & chop 67 2.12 Curly braces 70 2.13 Additional Resources 71 2.14 Lab Exercises 72

Trang 4

Table of Contents ©2018 One Course Source, Inc Page 4

3.1 Array Variables 74 3.2 Referencing Array Elements 75 3.3 Adding & removing elements in an array 79 3.4 The splice function 82 3.5 Using for loops 85 3.6 Using foreach loops 87 3.7 The reverse statement 91 3.8 The sort operator 92 3.9 The qw and qq statements 95 3.10 Arrays used in scalar context 96 3.11 Additional Resources 97 3.12 Lab Exercises 98

4.1 Associative Array Variables 100 4.2 Creating associative arrays 102 4.3 Accessing values with keys and foreach 105 4.4 Accessing values with "while-each" loops 107 4.5 The values statement 112 4.6 Reverse searching an associative array 114 4.7 Removing associative array keys and values 115 4.8 exists vs defined 116 4.9 Special Variables 119 4.10 The Environment Variables 120 4.11 The Argument Variable 122 4.12 Additional Resources 123 4.13 Lab Exercises 124

Trang 5

Table of Contents ©2018 One Course Source, Inc Page 5

5.1 Blocks 126 5.2 The if Statement 127 5.3 The unless Statement 133 5.4 The switch statement 134 5.5 The given statement 137 5.6 The while Statement 139 5.7 The until Statement 173 5.8 The do Statement 141 5.9 Loop control: last 144 5.10 Loop control: next 148 5.11 Additional Resources 149 5.12 Lab Exercises 150

6.1 Numeric Comparison 152 6.2 String Comparison 153 6.3 Difference between string and numeric comparison 155 6.4 Pattern Matching 158 6.5 Using the Outcome of a Statement 159 6.6 File Test Conditions 161 6.7 Complex Conditional Expressions 165 6.8 Understand and/or versus &&/|| 168 6.9 Using parentheses 170 6.10 Short circuiting 171 6.11 Additional Resources 173

6.12 Lab Exercises 174

Trang 6

Table of Contents ©2018 One Course Source, Inc Page 6

7.1 Reading Input 176 7.2 while & until Loops 177 7.3 Record Separator Variable 179 7.4 The Diamond Operator 181 7.5 The Default Variable 184 7.6 Using parentheses 186 7.7 Additional Resources 187 7.8 Lab Exercises 188

8.1 Filehandles 190 8.2 The die and warn Statements 191 8.3 Opening and Reading from Files 195 8.4 Opening and Writing to Files 198 8.5 Reading a Block of a Filehandle 200 8.6 Reading a Single Character 202 8.7 Piping in Perl 203 8.8 The format Statement 207 8.9 Here Documents 219 8.10 Additional Resources 222 8.11 Lab Exercises 223

9.1 Pattern Matching vs Wildcards 225 9.2 Matching, Substituting and Translation 226 9.3 Modifiers 229 9.4 Regular Expressions: Metacharacters 232 9.5 Regular Expressions: Classes 249 9.6 Regular Expressions: Backreferencing 256 9.7 Additional Resources 263 9.8 Lab Exercises 264

Trang 7

Table of Contents ©2018 One Course Source, Inc Page 7

10.1 split 266 10.2 join 270 10.3 substr 272 10.4 index 275 10.5 rindex 276 10.6 grep 278 10.7 srand & rand 280 10.8 sleep 282 10.9 Additional Resources 284 10.10 Lab Exercises 285

11.1 Controlling the Filesystem within Perl 287 11.2 Working with Directories 289 11.3 Working with Files 294 11.4 Back-Quoting 298 11.5 The System Statement 299 11.6 Additional Resources 301 11.7 Lab Exercises 302

12.1 Creating Functions 304 12.2 Invoking Functions 305 12.3 Returning Values from Functions 307 12.4 Passing Parameters 310 12.5 Scope of Variables 314 12.6 local() vs my() 316 12.7 Additional Resources 324 12.8 Lab Exercises 325

Trang 8

Table of Contents ©2018 One Course Source, Inc Page 8

13.1 What are Modules? 327 13.2 Loading Modules with use 328 13.3 Other functions of use 330 13.4 Additional Resources 335 13.5 Lab Exercises 336

14.1 The -w switch 338 14.2 The Perl Debugger 341 14.3 Debugger Commands 342 14.4 Additional Resources 343 14.5 Lab Exercises 344

Trang 9

Introduction ©2018 One Course Source, Inc Page 9

Introduction About this course

This manual was designed with the goal of assisting instructors in their

efforts of teaching students to be able to create Perl programs

Typographical syntax

Examples in this text of commands will appear in bold text and the output

of the commands will appear in italic text The commands and the output

of the commands will be placed in a box to separate them from other text

Example:

[student@ocs student]$ pwd

/home/student

Note: "[student@ocs student]$" is a prompt, a method the shell uses to

say “I’m ready for a new command”

Bold text within a sentence will indicate an important term or a command

Files and directories are highlighted by being placed in courier font

Trang 10

Introduction ©2018 One Course Source, Inc Page 10

Using this manual while in class

In many ways, class manuals are different from textbooks Textbooks are

often filled with lengthy paragraphs that explain a topic in detail

Unfortunately, this style doesn’t work well in a classroom environment

Class manuals often are much more concise than textbooks It's difficult to

follow the instructor’s example and read lengthy paragraphs in a book at

the same time For this purpose, class manuals are often more terse

Lab Exercises

The lab exercises provided in this class are intended to provide practical,

hands on experience with programming Perl Students are strongly

encouraged to perform the labs provided at the end of each Unit to

reinforce the knowledge provided in class

Trang 11

Introduction ©2018 One Course Source, Inc Page 11

Resources

In each unit, resources are provided to provide the learner with a source

for more information These resources may include online documentation,

local (on your system) documentation and traditional books

Due to the fact that these resources changes (especially online resources),

we provide these resources on our web site instead of printing them in our

courseware This allows us to provide updated resources in a timely

manner

For this class, the resources can be found here:

Trang 12

Introduction ©2018 One Course Source, Inc Page 12

Using the Perl debugger

During this course the Perl debugger is used to demonstrate techniques

To enter the Perl debugger to test Perl code, use the following:

ocs% perl -d -e "1;"

Loading DB routines from perl5db.pl version 1.32

Editor support available

Enter h or `h h' for help, or `perldoc perldebug' for more help

main::(-e:1): 1;

DB<1>

Note: if you installed Active State's ActivePerl, you might end up in their

GUI-based debugger To temporarily change your system to use the

built-in Perl debugger, enter the followbuilt-ing command(s) built-in your shell:

Windows

set PERL5DB=BEGIN { require 'perl5db.pl'; }

set PERLDB_OPTS=

Trang 13

Introduction ©2018 One Course Source, Inc Page 13

Linux

export PERLDB_OPTS=

To re-enable the PDK debugger, set the PERL5DB variable to an empty

string

A note about this course content

In most cases it is best to use the latest version of Perl However, using

the latest version is not always going to be possible Many students who

follow this course or take official OCS classes will be in an environment

that uses an older versions of Perl (sometimes a much older version)

There are a variety of reasons (being stuck on an older OS platform,

needing a feature that only exists in an older version of Perl, stubborn IT

managers, etc.) why you may be required to use an older version of Perl

This was taken into consideration when this manual was written While the

newer Perl techniques are covered in this class and are normally more

effective, the course also presents older techniques Not only does this

help support Perl programmers who are stuck in older versions of Perl, but

it also helps Perl programmers who have been called upon to maintain (or

update) older Perl scripts

In addition, there are many things in Perl that can be accomplished in more

than one way While not every single method is provided in this course, we

do try to show different common ways of performing tasks This makes it

easier for you to read other Perl scripts that may have used a different

technique than what you are using

Perl is well known for being a flexible programming language While there

are some advantages of this, it does require exploring more techniques in

a course such as this one

Trang 14

Introduction ©2018 One Course Source, Inc Page 14

Try it!

Throughout the course, you will see boxes like this one The purpose of

these boxes is to encourage you to try a new skill that is being taught

Keep in mind that in an instructor-led classes, these boxes may be ignored

by the instructor The benefit of these boxes is for self-study learners or for

learners who are reviewing the materials after the end of the instructor-led

class

Trang 15

1 – Perl Fundamentals ©2018 One Course Source, Inc Page 15

Unit One Meet Perl

Trang 16

1 – Perl Fundamentals ©2018 One Course Source, Inc Page 16

Trang 17

1 – Perl Fundamentals ©2018 One Course Source, Inc Page 17

1.2 Perl development environments

Trang 18

1 – Perl Fundamentals ©2018 One Course Source, Inc Page 18

Which derivative for *nix?

Trang 19

1 – Perl Fundamentals ©2018 One Course Source, Inc Page 19

Which derivative for Windows?

Trang 20

1 – Perl Fundamentals ©2018 One Course Source, Inc Page 20

Pick your Perl development tools

Trang 21

1 – Perl Fundamentals ©2018 One Course Source, Inc Page 21

Trang 22

1 – Perl Fundamentals ©2018 One Course Source, Inc Page 22

Understanding Perl versions

Trang 23

1 – Perl Fundamentals ©2018 One Course Source, Inc Page 23

Trang 24

1 – Perl Fundamentals ©2018 One Course Source, Inc Page 24

Trang 25

1 – Perl Fundamentals ©2018 One Course Source, Inc Page 25

The interactive method (debugger)

Trang 26

1 – Perl Fundamentals ©2018 One Course Source, Inc Page 26

Note: if you installed Active State's ActivePerl, you might end up in their

Trang 27

1 – Perl Fundamentals ©2018 One Course Source, Inc Page 27

Trang 28

1 – Perl Fundamentals ©2018 One Course Source, Inc Page 28

The script method

Trang 29

1 – Perl Fundamentals ©2018 One Course Source, Inc Page 29

UNIX-based operating systems provide you another method of executing

Trang 30

1 – Perl Fundamentals ©2018 One Course Source, Inc Page 30

regarding Perl is found

under the "Manual"

section

When you want to view

documentation about a

specific function, variable

or feature, look under the

Reference section

Standard modules (which are like libraries), have documentation that can

be viewed under the Modules section

Trang 31

1 – Perl Fundamentals ©2018 One Course Source, Inc Page 31

In addition to the web-based documentation, you can access

Trang 32

1 – Perl Fundamentals ©2018 One Course Source, Inc Page 32

On Windows, execute the command perldoc perl:

Trang 33

1 – Perl Fundamentals ©2018 One Course Source, Inc Page 33

If you review the main documentation page (perldoc perl), you will discover

Trang 34

1 – Perl Fundamentals ©2018 One Course Source, Inc Page 34

Trang 35

1 – Perl Fundamentals ©2018 One Course Source, Inc Page 35

To see documentation on a specific builtin function, use the -f option with

Trang 36

1 – Perl Fundamentals ©2018 One Course Source, Inc Page 36

1.6 Perl Resources

In each unit, resources are provided to provide the learner with a source

for more information These resources may include online documentation,

local (on your system) documentation and traditional books

Due to the fact that these resources changes (especially online resources),

we provide these resources on our web site instead of printing them in our

courseware This allows us to provide updated resources in a timely

manner

For this unit, the resources can be found here:

Trang 37

1 – Perl Fundamentals ©2018 One Course Source, Inc Page 37

1.7 Lab Exercises

A note about the lab exercises in this class: Creating lab exercises that

will be beneficial to all students can be difficult Lab exercises that focus

on specific scenarios (such as engineering test cases) can result in

difficulties for students who do not perform this sort of programming As a

result, we focused on creating labs that will perform tasks that are fairly

generic, but that also assist the student in practicing the new skills that are

learned in class In addition, to make the lab exercises more realistic, we

attempt to build on one script throughout class rather than build many,

small scripts

Throughout this course, you will build on a script called "cb.pl"

This script will eventually be a simple (flat) database program that will be

customized to fit simple database needs To make it specific for the class,

we will make it a database that contains checkbook entries

For this lab, create a file called cb1.pl and perform the following functions:

1 Create comments at the beginning of the code that indicate what the

program does (handles deposits, withdrawals, checks writing, looks

up checks by check number or date written, and prints a statement)

and other data (author, date/time, version (1.1))

2 Using the print statement, have the program produce the following

output when it is run:

Welcome to checkbook 1.1

Please enter your name:

When you have completed your work, compare your script against the

cb1.pl file provided in lab answers During the instructor-led classes, the

instructor may review the answer script and provide you with the

opportunity to ask questions

Trang 38

2 – Scalar Variables ©2018 One Course Source, Inc Page 38

Unit Two Scalar Variables

2.9 Auto increment and Auto decrement 61

2.10 Reading data from the user 66

2.11 chomp & chop 67

2.12 Curly braces 70

2.13 Additional Resources 71

2.14 Lab Exercises 72

Trang 39

2 – Scalar Variables ©2018 One Course Source, Inc Page 39

Trang 40

2 – Scalar Variables ©2018 One Course Source, Inc Page 40

operators can only be used

on scalar variables (not constant numbers)

Auto decrement See later

Ngày đăng: 04/03/2019, 16:40

TỪ KHÓA LIÊN QUAN