Chapter 2. Perl Quick Start

Image

2.1 Quick Start, Quick Reference

The following reference gives you a general overview of Perl constructs and syntax. It can be used later as a cheat sheet to help you quickly refresh your memory without searching through chapters for a simple concept.

2.1.1 A Note to Programmers

If you have had previous programming experience in another language (such as Visual Basic, C/C++, C#, Java, Python, or PHP), and you are familiar with basic concepts (such as variables, loops, conditional statements, and functions), Table 2.1 will give you a quick overview of the constructs and syntax of the Perl language.

Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image

Table 2.1 Perl Syntax and Constructs

At the end of each section, you will be given the chapter number that describes the particular construct and a short, fully functional Perl example designed to illustrate how that construct is used.

2.1.2 A Note to Non-Programmers

If you are not familiar with programming, skip this chapter and go to Chapter 5, “What’s in a Name?” You may want to refer to this chapter later for a quick reference.

2.1.3 Perl Syntax and Constructs

Table 2.1 summarizes the Perl concepts discussed throughout this book. If applicable, cross-references are given, as to where you can read further on these topics.

Regular Expressions

A regular expression is set of characters normally enclosed in forward slashes. They are to match patterns in text and to refine searches and substitutions. Perl is best known for its pattern matching (see Chapter 8, “Regular Expressions—Pattern Matching”). Table 2.2 shows a list of metacharacters and what they mean when used in a regular expression.

Image
Image

Table 2.2 Some Regular Expression Metacharacters

Passing Arguments at the Command Line

The @ARGV array is used to hold command-line arguments. If the ARGV filehandle is used, the arguments are treated as files; otherwise, arguments are strings coming in from the command line to be used in a script. (See Chapter 10, “Getting a Handle on Files.”)

References and Pointers

Perl references are also called pointers (although they are not to be confused with C language pointers). A reference is a scalar variable that contains the address of another variable. To create a reference, the backslash operator is used. References are used to pass arguments as addresses (pass by reference) to functions, create nested data structures, and create objects. (See Chapter 12, “Does This Job Require a Reference?” and Chapter 13, “Modularize It, Package It, and Send It to the Library!”)

Objects

Perl supports objects, a special type of reference. A Perl class is a package containing a collection of variables and functions, called properties and methods. There is no class keyword. The properties (also called attributes) describe the object. Methods are special functions that allow you to create and manipulate the object. Objects are created with the bless function (see Chapter 14, “Bless Those Things! (Object-Oriented Perl).”

Creating a Class
Instantiating a Class

Perl also supports method inheritance by placing base classes in the @ISA array.

Libraries and Modules

Library files have modules and “module” is used to refer to a single .pm file inside the library. The standard Perl library, prior to version 5.18, included files with the .pl extension. Today, .pm files are more commonly used than .pl files (see Chapter 13, “Modularize It, Package It, and Send It to the Library!”).

Path to Libraries

@INC array contains list of paths to standard Perl libraries and can be updated.

To Include a File

To load an external file, the use function imports a module and an optional list of subroutine or variable names into the current package.

use Moose;  # Loads Moose.pm module at compile time

Diagnostics

To exit a Perl script with the cause of the error, you can use the built-in die function or the exit function.

You can also use the Perl pragmas:

use warnings;  # Provides warning messages; does not abort program
use diagnostics; # Provides detailed warnings; does not abort program
use strict; # Checks for global variables, unquoted words, etc.;
            # aborts program

2.2 Chapter Summary

This chapter was provided for programmers who need a quick peek at what Perl looks like, its general syntax, and programming constructs. It is an overview. There is a lot more to Perl, as you’ll see as you read through the following chapters.

Later, after you have programmed for a while, this chapter can also serve as a little tutorial to refresh your memory without having to search through the index to find what you are looking for.

2.3 What’s Next?

In Chapter 3, “Perl Scripts,” we will discuss Perl script setup. We will cover how to name a script, execute it, and add comments, statements, and built-in functions. We will also see how to use Perl command-line switches and how to identify certain types of errors.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.214.27