Beginning Perl Programming Self Study Guide & Student Manual 2018 19 Edition pdf pdf

  

Beginning Perl

Programming

Self-Study Guide & Student Manual

  2018-19 Edition William "Bo" Rothwell

  A publication by

  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.

  

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

Module Table

of Contents

  Introduction Unit One Meet Perl

  Page

1.1 ......................................................................... Origin of Perl

  

1.2 ........................................... Perl development environments

1.3 ......................................................................... Perl Versions

1.4 ......................................................................... Invoking Perl

1.5 ............................................................... Perl Documentation

1.6 ......................................................................Perl Resources

1.7 ........................................................................Lab Exercises

   Unit Two Scalar Variables Page

  

2.1 .................................................................... Numeric Literals

  

2.2 .......................................................... Manipulating Numbers

2.3 ........................................................................ String Literals

2.4 .............................................................. Manipulating Strings

2.5 .............................................................. Strings vs. Numbers

2.6 ...................................................... Single vs. Double Quotes

2.7 .................................................................. Scalars Variables

2.8 .............................................................. Undefined variables

2.9 ..................................... Auto increment and Auto decrement

2.10 ................................................. Reading data from the user

2.11 ..................................................................... chomp & chop

2.12 ........................................................................ Curly braces

2.13 .......................................................... Additional Resources

2.14 ......................................................................Lab Exercises

  

  Unit Three Array Variables Page

  3.1 ..................................................................... Array Variables 3.3 ............................... Adding & removing elements in an array 3.5 ...................................................................... Using for loops 3.7 .......................................................... The reverse statement 3.9 .................................................... The qw and qq statements 3.11 .......................................................... Additional Resources

  Unit Four Associate Array Variables Page

  4.1 ................................................... Associative Array Variables 4.3 ............................... Accessing values with keys and foreach 4.5 ............................................................ The values statement 4.7 ........................ Removing associative array keys and values 4.9 ................................................................. Special Variables 4.11 ..................................................... The Argument Variable 4.13 ......................................................................Lab Exercises

  

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

  Unit Five Flow Control Page

  5.1 ................................................................................... Blocks 5.3 ........................................................... The unless Statement 5.5 .............................................................. The given statement 5.7 ............................................................... The until Statement 5.9 ................................................................... Loop control: last 5.11 .......................................................... Additional Resources

  Unit Six Conditional Expressions Page

  6.1 .............................................................Numeric Comparison 6.3 .............. Difference between string and numeric comparison 6.5 ....................................... Using the Outcome of a Statement 6.7 ......................................... Complex Conditional Expressions 6.9 ................................................................ Using parentheses 6.11 .......................................................... Additional Resources

  

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

  Unit Seven Basic Input and Output Page

  7.1 ........................................................................ Reading Input 7.3 .................................................... Record Separator Variable 7.5 ............................................................. The Default Variable 7.7 ............................................................ Additional Resources

  

Unit Eight Advanced Input and Output Page

8.1 ............................................................................ Filehandles 8.3 ........................................... Opening and Reading from Files 8.5 ............................................ Reading a Block of a Filehandle 8.7 ......................................................................... Piping in Perl 8.9 ................................................................... Here Documents 8.11 ......................................................................Lab Exercises

  Unit Nine Pattern Matching Page

  9.1 ............................................. Pattern Matching vs. Wildcards 9.3 ............................................................................... Modifiers 9.5 .............................................. Regular Expressions: Classes 9.7 ............................................................ Additional Resources

  

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

  Unit Ten Perl Utilities Page

  10.1 ..................................................................................... split 10.3 .................................................................................. substr 10.5 .................................................................................. rindex 10.7 ....................................................................... srand & rand 10.9 .......................................................... Additional Resources

  

Unit Eleven Filesystem & Process Control Page

11.1 .................................. Controlling the Filesystem within Perl 11.3 ............................................................... Working with Files 11.5 ........................................................ The System Statement 11.7 ......................................................................Lab Exercises

  Unit Twelve Functions Page

  12.1 .............................................................. Creating Functions 12.3 ........................................ Returning Values from Functions 12.5 .............................................................. Scope of Variables 12.7 .......................................................... Additional Resources

  

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

  Unit Thirteen Using Modules Page

  13.1 ............................................................. What are Modules? 13.3 ......................................................... Other functions of use 13.5 ......................................................................Lab Exercises

  Unit Fourteen Debugging Perl Page

  14.1 ...................................................................... The -w switch 14.3 ......................................................... Debugger Commands 14.5 ......................................................................Lab Exercises

  

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

  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.

  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.

  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:

  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 following command(s) in your shell: Windows set PERL5DB=BEGIN { require 'perl5db.pl'; } set PERLDB_OPTS=

  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.

  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.

  1

  ©2018 One Course Source, Inc. Page 15

  Unit One Meet Perl Unit topics:

  Page

1.1 ......................................................................... Origin of Perl

  

1.2 ........................................... Perl development environments

1.3 ......................................................................... Perl Versions

1.4 ......................................................................... Invoking Perl

1.5 ............................................................... Perl Documentation

1.6 ......................................................................Perl Resources

1.7 ........................................................................Lab Exercises

  

  1

1.1 Origin of Perl

  2

  3 Perl was developed in 1987 by Larry Wall. It was created because the

  4

  tools that were available to Mr. Wall at the time (sed, C, awk and the

5 Bourne Shell) didn’t provide the sort of functionality that he required.

  6

  7 Perl was initially called Pearl, but the name was quickly changed due to the 8 fact that there was another language called Pearl at the time. Perl is a

  9

  

backronym (a constructed acronym created to fit an existing word) that

  10

  stands for "Practical Extraction & Report Language”. Some programmers

  11

  (typically those who don’t like Perl) claim that Perl stands for 12 "Pathologically Eclectic Rubbish Lister".

  13

  14 Perl code is mostly machine-independent. This means that you can write a

  15 Perl program on one platform (like Linux) and then easily use the same 16 program on another platform (like Windows). When dealing directly with 17 the Operating System or filesystem, you may need to make some changes

  18 to your code.

  19

  20 Perl has been ported to UNIX, Windows, Linux and many others (see

  21 http://www.cpan.org/ports for a complete list).

  22

  1 ©2018 One Course Source, Inc. Page 16

1.2 Perl development environments

2 In some cases you might not have a choice as to which platform or

  8

  1

  3

  derivative of Perl that you will use to develop your code. However, if you

  4

  

do have a choice, you should spend some time learning the differences

  5 between your options.

  6

  7

  • *nix/Windows
  • *nix refers to any UNIX-based OS (including Linux). Many developers

    • • UNIX-based systems typically have more powerful features for

    • UNIX-based systems tend to be more stable than Window-based

  9

  20

  ©2018 One Course Source, Inc. Page 17

  27

  26

  25 http://perldoc.perl.org/perlport.html.

  want to review the following document:

  24

  23 In either case, if you are worried about writing portable code, you probably

  22

  21 deciding your development platform.

  of Windows over *nix). Consider the pros and cons of each before

  19 There are other advantages (and some would argue there are advantages

  10

  18

  17 systems.

  16

  15

systems typically don't have a C/C++ compiler by default.

  14 C++ compiler, making it easier to install CPAN modules. Windows

  1

  13

  12

  11

  

prefer this environment over Windows for several reasons, including:

  developers. For instance, most UNIX-based systems have a C or

  1 Which derivative for *nix?

  2

  3 If you decide to develop on a UNIX-based platform, you may want to

  4

  

consider which derivative of Perl to install and develop on. Unless you

  5

  want to create a custom build of Perl (well beyond the scope of this class*),

  6 your choice will likely come down to two: the standard Perl derivative

  7

  (www.perl.org) or ActiveState's ActivePerl 8 (http://www.activestate.com/activeperl).

  9

10 When you consider which derivative to use, take the following into account:

  11 12 • Most *nix systems have Perl installed by default as several system 13 tools (especially on Linux) make use of Perl to manipulate data. In 14 these cases, you are likely to find the standard Perl installed (or a 15 custom build for that Linux distribution). 16 • ActivePerl comes with a tool to easily install Perl modules: ppm.

  17 This tool is normally considered easier to use than installing CPAN

  18 modules with the -MCPAN option. 19 • ActivePerl's ppm doesn't install modules directly from CPAN, but 20 rather from another repository that ActiveState maintains. This 21 means you have access to a subset of the CPAN modules, not the

  22

  complete set. Note: you can choose additional repositories by

  23 clicking "Edit" + "Preferences" and choosing the "repositories" tab.

  24 25 *If you do want to create your own custom Perl derivative, you probably

  26

  want to start by looking at the following: http://search.cpan.org/dist/App- 27 perlbrew/.

  28

  1 ©2018 One Course Source, Inc. Page 18

  1 Which derivative for Windows?

  2

  3 If you are working on a Window's platform, you have a few choices 4 available, as described on www.perl.org: 5 6 • ActiveState Perl has binary distributions of Perl for Win32 (and Perl 7 for Win64). 8 • Strawberry Perl: A 100% Open Source Perl for Windows that is 9 exactly the same as Perl everywhere else; this includes using

  10 modules from CPAN, without the need for binary packages. (see 11 http://strawberryperl.com/) 12 • DWIM Perl for Windows: A 100% Open Source Perl for Windows, 13 based on Strawberry Perl. It aims to include as many useful CPAN

  14

  modules as possible. It even comes with It even comes with Padre,

  15 the Perl IDE. (see http://dwimperl.com/windows.html)

  16

17 A few things to consider:

  18 19 • ActivePerl has ppm, Strawberry Perl does not. However, Strawberry

  20 Perl has many CPAN modules installed by default (both a pro and a

  21 con).

  22

  • With ActivePerl you can get official support. Strawberry Perl provides 23 only community support.

  24 • Strawberry Perl comes with gcc, a C/C++ complier, making it easier 25 to install modules from CPAN.

  26 • Strawberry Perl release cycle tends to be slower than ActivePerl. 27 • "When I'm on Windows, I use Strawberry Perl." - Larry Wall

  28

  1 ©2018 One Course Source, Inc. Page 19

  1

  ©2018 One Course Source, Inc. Page 20

  Pick your Perl development tools

  1

  2 There are several good tools to help you develop your Perl code. This

  3

  includes debuggers, editors and IDEs. Some of these tools are free, some

  4

  can be very expensive. Many of them are community support while a few

  5 are commercially supported.

  6

  7 A good place to start exploring these tools is the following web site:

  8

  http://www.perlmonks.org/?node_id=531175

  9

  10

1.3 Perl versions

  2

  3

  1 To verify Perl is installed, and to show the version, type the command perl

  • -v:

  ©2018 One Course Source, Inc. Page 21

  26

  21 Internet, point your browser at http://www.perl.org/, the Perl Home Page.

  22

  23 You will see by the highlighted section above that Perl 5.10.1 is used on

  24

  this system. As of the date of when this was written, this version is

  25

  considered a bit "old", however it is important to note that many Perl

  developers are still using older versions of Perl. They may be "stuck" with

  20

  27 an older version because of platform issues or "related" software issues.

  28 This manual isn't written for a specific version of Perl 5, however notes will

  29 be made when a "newer" concept is covered.

  30

  31 Perl 5.10.1 is specifically used in this course because of a change that

  32 took place in that version.

  33

  34

  this system using "man perl" or "perldoc perl". If you have access to the

  19 Complete documentation for Perl, including FAQ lists, should be found on

  4

  10

  [student@ocs student]$ perl -v

  5

  6 This is perl, v5.10.1 built for MSWin32-x86-multi-thread

  7

  (with 4 registered patches, see perl -V for more detail)

  8

  9 Copyright 1987-2009, Larry Wall

  11 Binary build 1008 [294165] provided by ActiveState

  18

  12

  http://www.ActiveState.com

  13 Built Dec 9 2010 06:00:35

  14

  15 Perl may be copied only under the terms of either the Artistic License or

  16

  the

  1

  17 GNU General Public License, which may be found in the Perl 5 source kit.

  1 Understanding Perl versions

  2

  3 Perl version numbers sometimes are confusing. The first version of Perl 5

  4

  (5.000) was released in 1994. Initially Perl version numbers following the

  5

  numbering convention of 5.000, 5.001, 5.002, etc. When a minor change

  6 or "bug fix" release occurred, the numbering included this as 5.002_001 or 7 5.002_002.

  8

  9 The last release that followed this convention was 5.005_63; the next

  10

  release was 5.6.0. The primary reason for this numbering change was to

  11

  

fall in line with the version numbering system that most Open Source

12 projects followed.

  13

  14 Additionally, odd number releases (5.7, 5.9, 5.11, etc.) are considered

  15

development releases and should not be used for "real" programming.

  16 Previous release: 5.005_63

  17 Development releases Production releases

  18

  5.6

  19

  5.7

  5.8

  5.9

  5.10

  20

  5.11

  5.12

  21

  5.13

  5.14

  22

  5.15

  5.16

  23

  5.17

  5.18

  24

  5.19

  5.20

  25

  26 Which version of Perl 5 should you use? In some cases you may not have 27 a choice as your organization may have a specific version of Perl that you 28 must use. However, if the choice is yours, consider the following:

  29

  30 • Newer versions have more features than older versions. 31 • Only the latest version and the previous production release are 32 supported. Any older version is no longer actively maintained.

  33

34 This course was specifically written to address subtle differences in

  35

  different Perl 5 versions. Most of the material should work fine in Perl 5.6

  36 and higher. When there are differences, they will be mentioned in the 37 course materials.

  38

  1 ©2018 One Course Source, Inc. Page 22

  2

1 Try it!

  3 Whenever you program in Perl, it is very helpful to know what version of

  4 Perl you are using. Execute the following command to determine the 5 version of Perl that you are currently using:

  6

  7

  perl

  • –v

  8

  9

  1 ©2018 One Course Source, Inc. Page 23

  1

1.4 Invoking Perl

  2

  3 There are three methods to invoke Perl: the command line method, the 4 interactive method and the script method.

  5

  6 The command line method

  7

  8 Although this method is the least common way of invoking Perl, it does 9 provide a means of "testing" simple Perl statements. The -e option allows 10 the user to enter the Perl statements on the command line:

  11

  12

  [student@ocs student]$ perl -e 'print "This is my first perl program\n";'

  13 This is my first Perl program

  14

  15 Notes:

  16

  17

  ➢ The print statement will display its arguments to STDOUT (standard

  18 output, usually the screen)

  19

  20

  ➢ The "\n" character represents a newline character

  21 22 ➢ The ";" character ends the print statement

  23

  24

  ➢ The single quotes around the Perl statement are needed to "protect"

  25

  special characters from the shell

  26 27 ➢ The double quotes are needed around the text that will be printed by

  28

  the print statement

  29

  30

  ➢ In the Win32 environment, use double quotes around the print,

  31 single around the text.

  32

  1 ©2018 One Course Source, Inc. Page 24

  1 The interactive method (debugger)

  2

  3 The interactive method makes use of the Perl debugger to allow you to

  4

  enter a Perl "shell" in which you can type Perl statements. This is useful

  5

  

not only for testing Perl scripts, but also for "interactively" testing Perl

6 statements.

  7

8 To enter the Perl debugger, use the -d option:

  9 10 [student@ocs student]$ perl -d -e "1;"

  11 DB<1>

  12

  13 Notes:

  14

  15

  

➢ The -e option allows you to enter the Perl statement(s) on the

  16 command line. The "1;" is a "dummy" statement that has no real 17 meaning (but provides Perl with valid code in order to enter the 18 debugger).

  19 20 ➢ Instead of specifying -e "1;" you could specify a script name to 21 debug the script.

  22

  23

  ➢ Many of the examples given in this course are performed within the

  24

  debugger. This allows the instructor to demonstrate a concept or 25 technique without having to create a full program.

  26

  27

  ➢ There are a (very) few statements/features that don't work in the Perl

  28 debugger.

  29

  1 ©2018 One Course Source, Inc. Page 25

  1

  set PERLDB_OPTS=

  16

  15

  14 string.

  12

  export PERLDB_OPTS=

  11

  10

  9 Linux

  8

  7

  ©2018 One Course Source, Inc. Page 26

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

  6

  5

  4 Windows

  3

  in Perl debugger, enter the following command(s) in your shell:

  2

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

  

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

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

  2

  1 Try it!

  3 Execute the following command to enter the Perl Debugger environment:

  4

  5

  perl

  • –d –e “1;”

  6

  7 At the debugger prompt, execute the following Perl statement:

  8

  9

  print 5 + 6;

  10

  11 Exit the debugger by executing the following Perl statement:

  12

  13

  quit

  14

  15

  1 ©2018 One Course Source, Inc. Page 27

  1 The script method

  2

  3 This method is the most common method; it allows you to place Perl

  4

  statements inside of a file and execute them. The following line of text can

  5

  be placed into a file:

  6 7 #1_first.pl 8 print "This is my first Perl program\n ";

  9

  10 And then executed by using the Perl command:

  11 12 [student@ocs student]$ perl 1_first.pl

13 This is my first Perl program

  14

  [student@ocs student]$

  15

  16 Note:

  17 ➢ The "#" character is for commenting.

  18

  1 ©2018 One Course Source, Inc. Page 28

5 The first line in the following script is the shbang. It must start with "#!" and

  ➢ On a Windows system the shbang is primarily treated as a comment

  [student@ocs student]$ ./1_second.pl

  17 This is my second Perl program

  18

  [student@ocs student]$

  19

  20 Notes:

  21

  22

  23

  [student@ocs student]$ chmod a+x 1_second.pl

  (unless you use #!perl followed by an option to Perl). The Win32

  24

  en vironment uses file extension association (associating the “.pl”

  25 extension with the Perl Command Line Interpreter).

  26

  27 ➢ The actual location of Perl may vary from one platform to another.

  28 The rest of the example in this course will use #!perl instead of

  29 attempting to guess the full path to the Perl executable.

  30

  16

  1

  ©2018 One Course Source, Inc. Page 29

  7

  UNIX-based operating systems provide you another method of executing

  1

  

the script. You can place a special line of code at the top of the script

  2

  called a "shbang" (or "shebang") that tells the OS which command to use

  3 to execute the script.

  4

  6

  then contain the path to the command that will execute the script:

  8

  14

  #!/usr/bin/perl

  9

  # 1_second.pl

  10

  print "This is my second Perl program\n ";

  11

  12 Now that the shbang has been added, just make the script executable and

  13

  run it like a program:

  15

  1

1.5 Perl Documentation

  2

  3 One of the great features of Perl is the quality of its documentation. This 4 documentation can be accesses from several different sources. One of 5 these sources is the web site perldoc.perl.org. While browsing this site,

  6

  there are a few things that you want to take into consideration:

  7 Each version of Perl will General information

  8 have different regarding Perl is found

  9 documentation. Be sure under the "Manual"

  10 to select the version you section. are working on.

  11

  12

  13 When you want to view Standard modules (which documentation about a are like libraries), have specific function, variable documentation that can or feature, look under the be viewed under the Reference section. Modules section.

  1 ©2018 One Course Source, Inc. Page 30

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

  2

  documentation on your one system. If you are working on UNIX or Linux,

  3 you can execute the command man perl:

  4

  5

  6

  7 Press the "ENTER" key to scroll down one line at a time, the "space bar"

  8

  

key to scroll down one page at a time and the "q" key to quit viewing the

  9 document.

  10

  1 ©2018 One Course Source, Inc. Page 31

  1 On Windows, execute the command perldoc perl:

  2

  3

  4

  5 Note: you can also execute the perldoc command on UNIX and Linux

  6

  

systems, however the man command doesn't exist on Windows systems

  7 (unless additional software is installed).

  8

  1 ©2018 One Course Source, Inc. Page 32

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

  2

  many other useful sub-catagories. The following highlights a few that you

  3 many consider taking the time to read through:

  4

  5 Document Description 6 perlintro Perl introduction for beginners 7 perlrequick Perl regular expressions quick start 8 perlretut Perl regular expressions tutorial

  9

  perlstyle Perl style guide

  10

  perlcheat Perl cheat sheet

  11 perltrap Perl traps for the unwary 12 perldebtut Perl debugging tutorial 13 perlsyn Perl syntax

  14

  15 Each of these can be viewed by either using the man command or the

  16

  perldoc command. For example, to see the Perl cheat sheet, execute the 17 perldoc perlcheat command.

  18

  1 ©2018 One Course Source, Inc. Page 33

  2

1 Try it!

  3 Execut

  

e the following command to display the “perl style guide”:

  4

  5

  perldoc perlstyle

  6

  7

  8

  1 ©2018 One Course Source, Inc. Page 34

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

  2

  

the perldoc command. For example, to view the documentation for the

  3 Perl print function, execute perldoc -f print:

  4

  5

  1 ©2018 One Course Source, Inc. Page 35

  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:

1 ©2018 One Course Source, Inc. Page 36

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.

  

1 ©2018 One Course Source, Inc. Page 37

  Unit Two Scalar Variables Unit topics:

  Page

2.1 .................................................................... Numeric Literals

  

2.2 .......................................................... Manipulating Numbers

2.3 ........................................................................ String Literals

2.4 .............................................................. Manipulating Strings

2.5 .............................................................. Strings vs. Numbers

2.6 ...................................................... Single vs. Double Quotes

2.7 .................................................................. Scalars Variables

2.8 .............................................................. Undefined variables

2.9 ..................................... Auto increment and Auto decrement

2.10 ................................................. Reading data from the user