Thursday, December 20, 2007

Perl Components

Describing language—whether coded, written, or spoken, is fundamentally difficult
because in order to understand the language components (nouns, verbs, adjectives)
you also need to need to understand the semantics that convert those components in
isolation into an understandable language that allows you to communicate. Unfortunately,
it’s impossible to describe those semantics without giving examples of their use!
As a rule, Perl lets you do what you want, when you want to, and, more or less,
how you want to. Perl is far more concerned about letting you develop a solution
that works than it is about slotting your chosen solution into a set of standards and
a rigid structure.
The core of any program are the variables used to hold changeable information. You
change the contents of those variables using operators, regular expressions, and functions.
Statements help to control the flow of your program and enable you to declare certain
facts about the programs you are running. If you can’t find what you want using the
base Perl function set, you can make use of a number of modules, which export a list
of variables and functions that provide additional information and operations. If you
want to work in a structured format, modules also support objects, methods, and object
classes. You can, of course, also make your own modules that use your own functions.
We’ll have a quick look at some of the elements and components within Perl that
will help when we start to look at these individual items in more detail in future chapters.
Variables
Variables hold variable pieces of information—they are just storage containers for
numbers, strings, and compound structures (lists of numbers and strings) that we
might want to change at some future point.
Perl supports one basic variable type, the scalar. A scalar holds numbers and strings,
so we could rewrite the simple “Hello World” example at the beginning of this chapter as
$message = "Hello World\n";
print $message;
In this example, we’ve assigned a literal to a variable called $message. When you
assign a value to a variable, you are just populating that variable with some information.
A literal is a piece of static information—in this case it’s a string, but it could have been
a number. By the way, when you assign information, you are assigning the value to theright of the assignation operator (the = sign) to the lvalue on the left. The lvalue is the
name given to a variable or structure that can hold information. Normally this is a
variable, but functions and objects are also types of lvalues.
You’ll notice in the preceding example that the variable, $message, has a “funny”
character at the beginning. In this case, it’s a dollar sign, and it identifies the variable as
being a scalar. You always use a dollar sign when accessing a scalar value. The way to
remember a scalar is that the $ sign looks like an “s”, for scalar!
There are also some compound variable types—namely the array and the hash.
The array is a list of scalar variables—thus we can store a list of days using
@days = ('Mon','Tue','Wed','Thu','Fri','Sat','Sun');
The leading character for an array is an @ sign (think “a” for array), and you always
access an array of one or more values using an @ sign. You access the values in an array
by the numerical index; the first value is at index 0, so to get the first day of the week
from the preceding list, we’d use $days[0]. Note the leading $ sign—this is required
because we are accessing the scalar value at index 0 from the array.
Perl also supports a hash—this is a list that uses not numerical indices, but instead a
string “key” to access each “value”—the so-called key/value pair. Hash variables start
with a % sign—think of the two “o” characters in the % as the key and value. Thus we
could create a hash that contains month names (as the keys) and the days in that month
(as the values):
%months = ('January' => 31,
...
'November' => 30,
'December' => 31);
Now all we need to do when we want to know how many days are in November is
access the value in the %months hash with a key of “November”:
print "Days in November:",$months{'November'},"\n";
Perl also supports some other types of variables, such as filehandles (which allow
us to read from and write to files) and typeglobs (which allow us to access a variable
via the internal symbol tables). We also use references, which just point to other
variables without actually containing a value themselves.
The special characters used to access variables are a vital part of the Perl
language—they enable us to identify the variables easily and let the programmer
and Perl know what sort of variable we are expecting to use.Operators
Operators perform some sort of operation on a value or variable. For example, the +
operator adds two numbers together:
$sum = 4 + 5;
Other operators allow you to perform other basic math calculations, introduce lists
of values (for use with functions and variables), and assign values to variables and
subroutines.
There are also operators that enable us to use regular expressions that can “match”
information contained within a string against an expression, or perform a substitution
so that we can replace and translate information without having to explicitly define
its contents.
We’ll be looking at Perl operators, and the core mechanics of how Perl takes a raw
script and interprets the contents, in Chapter 3.
Statements
Statements enable us to control the execution of our script—for example, we might use
the if statement to test the value of a variable or operation so that the script can make
an informed decision about what to do next. Other statements include the loops, which
allow us to repeat a process on the same piece of data or on a sequence of data. Statements
also include declarations, such as those that allow us to define variables and subroutines.
We’ll be covering statements and control structures in Chapter 5.
Subroutines (Functions)
When you want to perform an operation on a variable a number of times, or the same
operation on a number of variables, it makes sense to place that sequence of operations
into a subroutine or function. Now when you want to perform that operation, you send
the variable to the subroutine, and then use the value returned from that subroutine.
Perl includes a number of subroutines that perform different operations—including
the print subroutine, which sends information to the screen (or to a file). Other subroutines
built into Perl include those for opening and communicating with files, talking
over a network, or accessing information about the system. Other built-ins provide
simple ways for performing different operations on variables and values.Modules
Once you have a collection of subroutines that you find useful, then you’ll probably
want to use them in other scripts and applications that you build with Perl. You could
copy them to the new scripts, but a much better solution is to make your own modules.
These are the libraries that extend the functionality of Perl.
Perl comes with its own, quite extensive, set of modules that allow you to communicate
over a network (see Chapter 12), develop user interfaces (see Chapter 17), access external
databases (see Chapter 13), and provide an interface for communicating with a web
server and a client browser when developing web solutions (see Chapter 18).
If you can’t find what you want within the standard Perl distribution, then there
is a central repository of modules built by other programmers called CPAN. This
contains literally thousands of modules to handle everything from accessing data
sources through to handling XML (Extensible Markup Language).

No comments: