\chapter{Getting Started\label{chap:getstart}}

\section{What can Perl do?}

I understand it is a full wastage of time for you to have read through half of a book to find that it is not 
the one you are looking for. Therefore, I am going to let you know what you will learn by following this 
tutorial as early as possible. \\

If you are looking for a programming language to write an HTML editor that runs on the Windows platform, or 
if you would like to write a Web browser or office suite, then Perl does not seem to be an appropriate language for you. C/C++, 
Java or (if you are using Windows) Visual Basic are likely to be more appropriate choices for you. \\

Although it appears that Perl is not the optimum language for developing applications with a graphical user 
interface (but you can, with Perl/Tk or native modules like \mono{WIN::GUI}), it is especially strong in doing 
text manipulation and extraction of useful information. Therefore, with database interfacing it is possible to 
build robust applications that require a lot of text processing as well as database management. \\

Perl is the most popular scripting language used to write scripts that utilize the \term{Common Gateway 
Interface} (CGI), and this is how most of us got to know this language in the first place. A cursory look at 
the \link{http://www.cgi-resources.com}{CGI Resource Index} Web site provided me with a listing of about 3000 Perl CGI 
scripts, compared with only 220 written in C/C++, as of this writing. There are quite many free Web hosts 
that allow you to deploy custom Perl CGI scripts, but in general C/C++ CGI scripts are virtually only allowed 
unless you pay. In particular, there are several famous programs written in Perl worth mentioning here: \\

\begin{itemize}
\item {
\link{http://www.yabbforum.com}{YaBB} is an open source bulletin board system. While providing users with many 
advanced features that could only be found on commercial products, it remains as a free product. Many webmasters 
use YaBB to set up their BBS. Another popular BBS written in Perl is \link{http://www.ikonboard.com}{ikonboard},
featuring a MySQL/PostgreSQL database back-end.
}
\item {
Thanks to the powerful pattern matching functions in Perl, search engines can also be written in Perl with 
unparalleled ease. \link{http://perlfect.com/freescripts/search}{Perlfect Search} is a very good Web site 
indexing and searching system written in Perl.
}
\end{itemize}

You will learn more about Perl CGI programming in Chapter~\ref{chap:cgi} of this tutorial. \\

\section{Comparison with Other Programming Languages}

There are many programming languages in use today, each of which placing its emphasis on certain application
domains and features. In the following section I will try to compare Perl with several popular programming 
languages for the readers to decide whether Perl is appropriate for you. \\

\subsection{C/C++}

Perl is written in the C programming language. C is extensively used to develop many system software.
C++ is an extension of C, adding various new features such as namespaces, templates, object-oriented 
programming and exception handling etc. Because C and C++ programs are compiled to native code, startup times 
of C/C++ programs are usually very short and they can be executed very efficiently. Perl allows you to delegate
part of your program in C through the Perl-C XS interface. This Perl-C binding is extensively used by 
cryptographic modules to implement the core cryptographic algorithms, because such modules are 
computation-intensive. \\

While C/C++ is good for performance-critical applications, C/C++ suffers a number of drawbacks. 
First, C/C++ programs are platform dependent. A C/C++ program written on Unix is different from one on
Windows because the libraries available on different platforms are different. Second, because C/C++ is 
a very structured language, its syntax is not as flexible as scripting languages such as Perl, Tcl/Tk or (on Unix
platforms) bash. If you are to write two functionally equivalent programs in C/C++ and Perl, very likely
the C/C++ version requires more lines of code compared with Perl.
And also, improperly written C/C++ programs are vulnerable to memory leak problems where heap memory allocated
are not returned when the program exits. On a Web server running 24$\times$7 with a lot of visitors, a 
CGI script with memory leak is sufficient to paralyze the machine. \\

\subsection{PHP}

Perl has been the traditional language of choice for writing server-side CGI scripts. However, in recent 
years there has been an extensive migration from Perl to PHP. Many programmers, especially those who are 
new to programming, have chosen PHP instead of Perl. What are the advantages of PHP over Perl? \\

PHP is from its infancy Web-scripting oriented. Similar to ASP or JSP, it allows embedding of inline PHP code inside HTML documents
that makes it very convenient to embed small snippets of PHP code, e.g. to update a counter when a visitor
views a page. Perl needs Server Side Includes (SSI) or an additional package ``eperl'' to implement a similar functionality. Also, it inherits
its language syntax from a number of languages so that it has the best features of many different languages. It
mainly inherits from C/C++, and portions from Perl and Java. It uses I/O functions similar to that in C,
that are also inherited into Perl, so it is relatively easy for Perl programmers to migrate to PHP. \\

While PHP supports the object-oriented paradigm, most of its functionalities are provided through functions. 
When PHP is compiled the administrator decides the sets of functionalities to enable. This in turn determines 
the sets of functions enabled in the PHP installation. I'm personally sceptical of this approach, because in practice only a small
subset of these functions is frequently used. On the other hand, Perl only has a small set of intrinsic functions
covering the most frequently used functionalities. Other functionalities are delegated to modules which are
only installed and invoked as needed. As I will introduce shortly and in Appendix~\ref{chap:cpan}, 
the Comprehensive Perl Archive Network (CPAN) contains a
comprehensive and well-organized listing of ready-made Perl modules that you can install and use very easily. \\

\subsection{Java/JSP}

Sun Microsystems developed the Java language and intended to target it as a general purpose programming 
language. It is from the ground up object-oriented and platform independent. Functionalities are accessed
through the Java API, consisting of hierarchies of classes similar to that of Perl. Java Server Pages (JSP)
is a Web scripting environment similar to ASP except with a Java syntax. Similar to C/C++, the Java
syntax is very structured and thus are not as flexible as scripting languages like Perl. Also, Java itself
is not just an interpreter, it is a virtual machine over which programmers are totally abstracted from the underlying operating 
system platforms, which allows the Java API to be implemented on top of this platform-independent layer. For those
who have programmed in Java before, you will probably find that the Java Virtual Machine takes rather long time
to load, especially on lower-end systems with limited computational power. This defers the possibility of 
widespread deployment of Java programs. \\

While Perl is not strictly a general-purpose programming language like Java, I found it difficult to
compare Perl and Java because of their different natures. However, if confined to the purpose of Web server scripting, 
I generally prefer Perl to JSP for its flexibility and lightweight performance. Despite this, I feel that 
Java is a language that is feature-rich and if time allows, you are strongly encouraged to find out 
more about this stupendous language, which is expecting increasing attention in mobile and embedded
devices because of its platform independence. \\ 

\subsection{ASP}

Active Server Pages (ASP) is only available on Windows NT-series operating systems where Internet Information Services (IIS) 
is installed (although alternative implementations of ASP on other system architectures exist, e.g. Sun Chili!Soft ASP, which 
is a commercial product that runs on Unix, but generally considered not very stable). \\
 
Running on a Windows Web server, ASP can impose a tighter integration with Microsoft technologies, so that the use of, say, 
ActiveX data objects (ADO) for database access can be made a lot easier. However, IIS is especially vulnerable to remote attacks 
when operated as a Web server. Numerous service packs have been released to patch the security holes in IIS and Windows NT. 
However, new holes are still being discovered from time to time that makes the deployment of Windows NT/IIS as the Web server 
of choice not very favourable. On the other hand, Apache, the renowned Web server for Unix and now for other operating 
systems as well, has far less security concerns and are less susceptible to remote attacks. Apache also has the largest installation
base among all Web server software, taking up more than 60\% of the market share. \\

\section{What do I need to learn Perl?}

You don't need to pay a penny to learn and use Perl. Basically, a text editor that handles text-only files and 
a working installation of the Perl interpreter are all that you will need. \\

\image{!hbt}{0.8\hsize}{!}{images/gvim.eps}{Editing a Perl source file with GVIM, running on GNU/Linux}{fig:gvim}

Under Microsoft Windows, Notepad meets the minimum requirement. However, a whole page of code in black is not 
visually attractive in terms of readability. Some text editors have the feature of \term{syntax highlighting}, 
with different parts of a statement displayed in different colours. Good colouring makes the source files
more pleasurable to look at (such colouring is used for display 
only and will not be saved to file). However, avoid using word processors like Microsoft Word or Wordpad which add 
proprietary control codes on file save by default. The Perl interpreter does not recognize these special formats. 
If you have to use these word processors, ensure that your files are saved as plain text ASCII format so that the Perl 
interpreter can access them. \link{http://www.anyedit.org}{AnyEdit} and \link{http://www.ultraedit.com}{UltraEdit}
are nice text editors on the Windows platform. On Unix variants, emacs and vim are stupendous text editors featuring syntax 
highlighting profiles for most programming languages with a lot of powerful features. Fig.~\ref{fig:gvim} shows a 
screenshot of a Perl source file edited with GVIM, a port of vim that runs on Windows, X-Windows with the GTK library 
on Unix/Linux and many other platforms. This is my favourite text editor and is used to construct my entire Web site. \\

If you are using one of the mainstream operating systems, the perl interpreter can be downloaded from the 
download section of \link{http://www.perl.com}{perl.com}. perl.com is the official Web site for the Perl language 
and you can find the download links to all available interpreter versions there. Choose the version which matches 
your operating system. When you go to the download page you will see two versions, namely the stable production 
release and the experimental developer's release. The stable release is the version I recommend to new users, 
because the developer's version is for more advanced users to beta test the new version. It may still contain bugs 
and may give incorrect results. The files you have to download are under the heading ``binary distribution''. Do 
not download the source code distribution unless you know exactly how to compile and install them. In case you are 
using an operating system that is not listed, a good place to find a binary distribution for your operating system 
is the \link{http://www.cpan.org}{CPAN}, located at \link{http://www.perl.com/CPAN-local/ports/index.html}{here}, 
which contains a fairly comprehensive list of platforms on which Perl can run. \\

For Windows users, most probably you should download the \link{http://www.activestate.com/Products/ActivePerl}{Activestate} 
distribution of Perl. It is very easy to install, with some extra tools bundled in the package for easy installation of new modules. 
For GNU/Linux users, most probably Perl is already installed or available as RPM (Redhat Package Manager) or DEB 
(Debian packages) formats. As many Linux distributions already have builtin support for RPM packages, you may look 
at your installation discs and you are likely to find some RPM binaries for Perl if it is not yet installed. For 
other Unix systems, you may find tarballs containing the Perl binaries. If no binaries are available for your 
system, you can still build from sources by downloading the source distribution from the CPAN. To check if perl 
is installed on your system, simply open a terminal and type \mono{perl -v}. If Perl is installed you will have 
the version information of Perl installed displayed on screen. If error messages appear, you will need to install 
it. \\

Installation of Perl will not be covered in this tutorial, and you should look for the installation help 
documents for details. \\

\specialbox{NOTES}{%
Because Perl is an open source software, which releases the source code to the public for free, you 
will see the source code distribution listed. Yet for usual programming purposes there is no need to download the 
source files unless binary distributions are not available for your system. \\

An exception is if you are using one of the operating systems in the Unix family (including Linux). There are 
already compilation tools in these operating systems and you can manually compile Perl from sources and install it 
afterwards. However, note that compilation can be a very time-consuming process, depending on the performance of 
your system. If you are using Linux, binary distributions in the form of RPM or DEB packages can be installed 
very easily. Only if you cannot find a binary distribution for your platform that you are encouraged to install 
from source package. \\
}

\section{Make Good Use of Online Resources}

You may need to seek reference while you are learning the language. As a new user you are not recommended to 
start learning Perl by reading the man-pages or the reference manuals. They are written in strict technical 
parlance that beginners, especially those who do not have prior programming experience or basic knowledge in 
Computer Science, would find reading them real headaches. You are recommended to follow this tutorial (or other 
tutorials or books) to acquire some basic knowledge first, and these reference documents will become very useful 
for ambitious learners to know more about the language, or when you have doubt on a particular subject you may be 
able to find the answers inside. In this course I will try to cover some important terms used in the reference 
materials to facilitate your understanding of the text. For the time being, you may want to have several books on 
Perl for cross-referencing purposes. I have tried to write this tutorial in a way that beginners should find it 
easy to follow, yet you may need to consult these books if you have any points that you don't understand fully. \\

Although you are not advised to read the official reference documents too early, in some later parts I may refer 
you to read a certain manpage. A manpage, on Unix/Linux systems, is a help document on a particular aspect. To 
read a particular manpage, (bring up a terminal if you are in X-Windows) type \mono{man} followed by the name 
of the manpage, for example, \mono{man perlvar}, the perlvar manpage will be displayed. For other platforms, 
manpages may usually come in the format of HTML files. Consult the documentation of your distribution for details. 
There is an online version of the Perl official documentation at \link{http://www.perldoc.com}{perldoc.com}. 
It contains the Perl man pages as well as documentation of the modules shipped with Perl. In fact, there are now
several manpages that are targeted at novice programmers. For instance, the \link{http://www.perldoc.com/perl5.8.0/pod/perlintro.html}{perlintro}
manpage is a brief introduction to the fundamental aspects of the Perl language that you should master 
fully in order to claim yourself a Perl programmer. \\

You are also reminded of the vast varieties of Perl resources online. There are many Perl newsgroups on the 
USENET and mailing lists devoted to Perl. Your questions may be readily answered by expert Perl programmers there. 
Of course, try to look for a solution from all the resources you can find including the FAQs before you post! 
Otherwise, your question may simply be ignored. \link{http://www.perlmonks.org}{Perl Monks} is also a very useful 
resource to Perl users. \\

\link{http://dmoz.org/Computers/Programming/Languages/Perl}{dmoz.org} contains a nice selection of Perl-related 
sites. You can find a similar list of entries on \link{http://www.yahoo.com}{Yahoo!}. \\

\link{http://www.google.com}{Google} is the best search engine for programmers. You can usually get very accurate 
search results that deliver what you need. For example, by specifying the terms \textit{``Perl CGI.pm example''}, 
you will get screenfuls of links to examples demonstrating the various uses of the CGI.pm module. As you will see later, 
this module is the central powerhouse allowing you to handle most operations involving CGI programming with unparalleled 
ease. For materials not covered in this tutorial or other books, a search phrase can be constructed in a similar manner 
that allows you to find documentation and solutions to your questions at your fingertips. \\

Of course, don't forget to experiment yourself! \link{http://www.cpan.org}{CPAN}, the Comprehensive Perl Archive Network  
is a nice place where you can download a lot of useful modules contributed by other Perl programmers. By using these 
modules you can enforce code reuse, rather than always inventing code from scratch again. There are so many modules 
on the CPAN available that you would be surprised at how active the Perl community has been. Some CPAN modules are 
well-documented, some are not. You may need to try to fit the bits and pieces together and see if it works. This requires 
much time and effort, but you can learn quite a lot from this process. \\

\section{The Traditional ``Hello World'' Program}

Traditionally, the first example most book authors use to introduce a programming language is what is customarily
called a ``Hello World'' program. The action of this program is extremely simple --- simply displays the text 
``Hello World'' to the screen and does nothing else. For all examples in this tutorial of which source code are given 
in the text, you are encouraged to type them in yourself instead of executing the examples downloaded from my Web 
site, since it is more likely that by doing so you would understand the materials more quickly. Let's write a 
``Hello World'' program to see the procedures we take to develop a Perl program. \\

If you are on Windows, it is a good practice to check if the path to the Perl interpreter has been added to 
the path list in C:$\backslash$Autoexec.bat. In this way, you can change to the path containing your Perl source 
files and can run the interpreter without specifying its path. The setup program of your distribution would 
probably have done it for you. If it hasn't, append the path to the end of the list and end it with a semicolon. 
A typical path list looks like this, the last one in this example is the path to the perl interpreter (note that 
your path may be different): \\

\begin{lstlisting}[numbers=none]{}
SET PATH = C:\WINDOWS; C:\WINDOWS\COMMAND; C:\WINDOWS\SYSTEM; C:\PERL\BIN;
\end{lstlisting}

\specialbox{NOTES}{%
The use of Autoexec.bat is now obsolete starting from Windows 2000. Setting of environment variables 
should be carried out by right-clicking on the ``My Computer'' icon, and then choose the ``Properties'' option.
Now select the ``Advanced'' tab and then click on the ``Environment Variables'' button at the bottom. To make
the perl interpreter available to all users on the system, the path should be appended to the PATH variable in
the ``System variables'' section. If you modify the PATH variable in the ``User variables'' section, only the
user concerned (presumably you) will be affected. \\
}

For Unix/Linux, check your PATH variable and see if the directory containing the perl executable is present 
(usually /usr/bin). You can look at the list of paths by typing \mono{echo \$PATH} on the command line 
(be careful of exact capitalization!). Look for ``/usr/bin'' in the colon-separated values. On some systems, 
the path to perl would be ``/usr/local/bin'' or something else, so please check carefully. You may need to modify
the startup login scripts like .login, .bashrc, .profile etc. so that you don't need to set PATH or
specify the full path to perl every time if perl is installed at some weird locations. A convenient workaround
is to create a symbolic link in a directory included in PATH, e.g. /usr/bin that points to the perl executable. \\

\head{EXAMPLE}{}
\lstinputlisting[backgroundcolor=\color{CodeBackground}]{src/getstart/hello.pl}

\vspace{0.8cm}
Here we outline the steps to create this simple program on Windows and Linux. \\

\textbf{Microsoft Windows} \\
 
\begin{enumerate}
\item {
Open Notepad (or any other text editor you choose) and type in the source code shown above. Note that the line numbers 
on the left are for identification of lines only and do NOT type them into the text editor. Please make 
sure word wrap is disabled.
}
\item {
Save the file as hello.pl. A few text editors, like Notepad, usually append the ``.txt'' extension to the 
filename when saving. You may put a pair of double quotes around the filename to circumvent this behaviour.
Also, if you are using Windows 2000 or above and would like to use Notepad, please ensure that the file encoding is set
to ANSI instead of Unicode.
}
\item {
Bring up an MS-DOS prompt window and change to the directory containing your newly created file. Say if you have 
saved to "C:\bs perl\_examples", then type \mono{cd C:\bs perl\_examples} and press Enter. Put 
a pair of double quotes around the path if any directories in the path contains spaces (In fact I don't recommend 
placing Perl source files in directories with names containing spaces. It only complicates matters).
}
\item {
Execute the program by typing \mono{perl -w hello.pl} and press enter.
}
\end{enumerate}

\textbf{Unix or GNU/Linux} \\

\begin{enumerate}
\item {
Open any text editor (vim, emacs, pico, kedit ....) and type in the source code shown above. Note that the line numbers 
on the left are for identification only and do NOT type them into the text editor. Please make sure 
word wrap is disabled.
}
\item {
Save the file as hello.pl. Note that the path on line 1 has to match the path to perl on your system. 
Also, no spaces should precede the `\#' character and no empty lines are allowed before this special line (traditionally 
known as the `\term{shebang}' line).
}
\item {
If you are in X-Windows environment, bring up a terminal window. Change to the directory containing the newly 
created file using the \mono{cd} command.
}
\item {
In order to run it without specifying the perl interpreter, set the file access privilege to user executable by 
using the \mono{chmod} command. The command should be \mono{chmod u+x hello.pl} and press Enter.
}
\item {
Execute the program by typing \mono{./hello.pl} and then press Enter.
}
\end{enumerate}

\specialbox{NOTES}{
Even if you are using Unix/Linux, it is not absolutely needed to chmod your perl source files. In fact, you 
only need to make those source files executable if you want them to be directly invoked without specifying
the name of the interpreter (i.e. \mono{perl}). In this case, the shell will look at the first line 
of the file to determine which interpreter is used, so the \mono{\#!/usr/bin/perl} line must exist. Otherwise, the 
file cannot be executed. If you only intend to execute the file in Unix or Linux using \mono{perl -w filename.pl},
then filename.pl need not be given an executable permission. As you will learn later, 
you may have some Perl source files that are not invoked directly. Instead, they are being sourced from another 
source file and don't need to be executable themselves. For these files, you don't need to chmod them and the 
default permission is adequate. \\
}
 
If there is not any errors, you should see the words ``Hello, World!'' under the command prompt. For such a 
simple program it is not easy to make mistakes. If error messages appear, check carefully if you have left out 
anything, because a trivial mistake is sufficient to end up with some error messages if you are not careful. 
Also check if you are using the latest stable version of Perl 5. All examples in this tutorial have been tested with 
Perl 5.8.0 Win32 (ActiveState distribution) and Perl 5.8.0 on GNU/Linux, but it should work for other 
distributions or versions as well unless otherwise noted. \\

The \mono{-w} is an example of a \term{switch}. You specify a switch to enable a particular interpreter 
feature. \mono{-w} is specified so that warning messages, if any, are displayed on screen. Under no circumstances should 
this switch be omitted because it is important, especially as a beginner, to ensure that the code written is 
correct. It also helps catch some mistakes that are otherwise difficult to capture. This is explained in more
detail in Chapter~\ref{chap:eval}. \\

A Perl script consists of \term{statements}, and each statement is terminated with a semicolon (;). A statement 
is rather like a sentence in human languages which carries a certain meaning. For computer languages a statement 
is an instruction to be performed. \\

The core of the program is on line 5. It is this statement that prints the text delimited by quotation marks 
to the screen (in a more accurate parlance, the text is sent to the \term{standard output}, which is the 
screen by default). \mono{print()} is an example of a builtin \term{function}. A function usually accepts a number of
\term{parameters}, or \term{arguments}. Parameters serve to provide a function with additional pieces of data that are 
necessary for its operation. In the example, \mono{print()} takes the text as parameter in order to display it on screen. A set of basic functions is provided 
to you by Perl for performing different actions. Some of these functions will be introduced as you progress through
this tutorial. \\

Notice the strange \mono{\bs n} at the end? It is one of the \term{escape characters} which will be described 
later in more detail. \mono{\bs n} is used to insert a line break. Therefore, you see a blank line before returning 
to the command prompt. \\

Lines preceded by a \# (sharp) sign are \term{comments} and are ignored by the perl interpreter. A comment does not 
need to be on its own line, it can be put at the end of a line as well. In that case, the remaining of the line 
(starting from \# and until the end of the line) is regarded as a comment. Comments are helpful for you or other 
programmers who read your code to understand what it does. You can put anything you like as 
comments. Line 1 is also a comment as it is of interest to the shell only instead of the perl interpreter itself. 
The switch \mono{-w} here is the same as that specified under the command line. This is read together with the 
path to perl to enable the display of warnings. \\

\specialbox{GOOD PROGRAMMING PRACTICES}{

\textbf{Comments} \\

Comments are meant for human readers to understand the source code without the need of running the program 
once in your brain. This increases both the readability and maintainability of your source code. Many programmers 
are lazy to insert comments throughout the code. But it is very likely when you look at a piece of code wrote 
earlier you may not understand it anymore as it is very complicated without any comments in it. \\

Therefore, you should include comments in appropriate places. Usually for a single block of code performing 
one particular function we will place a comment briefly describing what this code block does. You may also want 
to place a comment on a particular line if the meaning of the line is not immediately obvious. Of course, don't 
deluge your source code with comments. For example, in the source code of the `Hello World' program I placed a 
comment for the \mono{print} statement. It is superfluous, in fact, as the meaning of this statement is pretty 
obvious. But I included it here because this is your first Perl function learnt. As you proceed, more 
constructive comments and less superfluous comments will be found in the examples. \\

%As Perl programs have to be distributed in source code (although an experimental tool exists that allows Perl programs
%to be distributed in standalone bytecode format), I have seen some programmers deter people from reading the 
%source code by using some script to remove all the newlines and garble variable names in order to make it nearly 
%impossible to understand. But such practices actually violate the principles of open source development, and I 
%am in opposition to such practices. \\
}


\section{How A Perl Program Is Executed}

Perl programs are distributed in source files. From the instant you invoke the perl interpreter to execute a
script, a number of steps were involved before the program is executed. \\

% perlhack

\paragraph{Preprocessing}
An optional preprocessing stage transforms the source file to the final form. This tutorial does not cover
source preprocessing. For details please consult the \link{http://www.perldoc.com/perl5.8.0/pod/perlfilter.html}{perlfilter} manpage. \\

\paragraph{Tokenizing}
The source file is broken down into tokens. This process is called tokenization (or \term{lexical analysis}).
Whitespaces are removed. Token is the basic unit that makes up a statement. By tokenizing the input parsing
is becoming easier because all further processing are carried out on tokens, independent of whitespace. \\

\paragraph{Parsing \& Optimization (Compilation)}
Parsing involves checks to ensure the program being executed conforms to the language specification and builds
a \term{parse tree} internally which describes the program in terms of microoperations internal to Perl (opcode).
Some optimizations to the parse tree are performed afterwards. Finally, the parse tree built is used to execute the program. \\

While in-depth understanding of any of these processes is not essential to practical Perl programming, the compilation
phase will be mentioned in some later chapters that I believe it is a good idea to briefly introduce the
phases involved beforehand. \\ 

\section{Literals}

All computer programs have to handle data. In every program there are certain kinds of data that do not 
change with time. For example, consider a very much simplified CGI script that checks if the password input 
by the user matches the system. How would you implement it? It seems the simplest method would be to have the 
correct password specified in the script, and after the user has entered the password and hit the ``Submit''
button, compare it against the password input by the user. The standard password specified in this script does 
not change during the course of execution of the script. This is an example of a \term{literal}. Other terms 
that are also used are \term{invariants} and \term{constants}. In the previous Hello World example, 
the text ``Hello, World!\bs n'' on line 5 is also a literal (This piece of data cannot be changed during the time you 
are running the program). \\

Literals can have a number of forms, just because we can have data of different forms. In Perl we can 
roughly differentiate numbers and strings. \\

\subsection{Numbers}

There are several classes of numbers: \term{integers} and decimals (known as \term{floating-point numbers} 
in computer literature). \\

In Perl, integers can be expressed in decimal (base 10), hexadecimal (base 16) or octal (base 8) notation. 
Octal numbers are preceded by a \mono{0} (zero), e.g. \mono{023} is equivalent to $19_{10}$ 
(the subscript 10 denotes representation in base-10, i.e. decimal form); hexadecimal numbers are preceded by 
\mono{0x} (zero x), e.g. \mono{0xfe} is equivalent to $254_{10}$. For hexadecimal digits A - F, 
it does not matter whether you specify them in lowercase or uppercase. That is, \mono{0xfe} is the same as 
\mono{0xFE}. \\

Integers cannot be delimited by commas or spaces like 10,203,469 or 20~300. However, Perl provides a nice 
workaround as a substitute. An example is 4\_976\_297\_305. This is just a facility to make large numbers easier 
to read by programmers, and writing 4976297305 is entirely correct in Perl. \\

Decimals are those carrying decimal points. If the integral portion is 0, the integral portion is optional, i.e. 
-0.6 or -.6 work equally fine. Exponents (base 10) can also be specified by appending the letter ``e'' and the exponent 
to the real number portion. e.g. \mono{2e3} is equivalent to 2 x 10\super{3} = 2000. \\

\subsection{Strings}

A \term{string} is a sequence of characters enclosed (delimited) by either double quotes (") or single quotes ('). They differ in 
variable substitution and in the way escape characters are handled. The text ``Hello, World!\bs n'' in the hello world example 
is a string literal, delimited by double quotes. \\

We will defer variable substitution until we come to variables. Escape characters exist in many programming languages. 
An escape character consists of a backslash \mono{\bs} symbol followed by a letter.
Every escape character has a function associated with it. Escape characters are usually put inside double-quoted strings. Table~\ref{tab:escape} 
summarizes the most important escape characters used in Perl. \\

\begin{table}[htbp]
\begin{center}
\begin{tabular}{cp{10.5cm}}
\hline
\textbf{Escape} \\ \textbf{Character}	&	\textbf{Function}	\\
\hline 
\br \\
	\bs n		&	Newline \\
				&	Starts a newline \br \\
	\bs r		&	Carriage Return \\
				&	Returns to the starting point of the line \br \\
	\bs t		&	Tab \\
				&	Analogous to striking the \textbf{Tab} key on your keyboard; However, using tab to make
				    formatter output does not always generate the format expected. \br \\
	\bs b		&	Backspace \\
				&	Analogous to the \textbf{Backspace} key; erases the last character \br \\
	\bs a		&	Bell \\
				&	Creates a beep sound from the system buzzer (or sound card) \br \\
	\bs x\mono{nn} &	ASCII character using hexadecimal notation \\
				& 	Outputs the character which corresponds to the specified ASCII index (each \mono{n} is a hexadecimal digit) \br \\
	\bs 0\mono{nn} &	ASCII character using octal notation \\
				&	Outputs the character which corresponds to the specified ASCII index (each \mono{n} is an octal digit) \br \\
	\bs c\mono{X}	&	Control Character \\
				&	For example, \mono{\bs cC} is equivalent to pressing Ctrl-C on your keyboard \br \\
	\bs u		&	Next letter	uppercase \\
				&	The letter immediately following \bs u is converted to uppercase. For example, \mono{\bs uemail} is equivalent to Email \br \\
	\bs l		&	Next letter lowercase \\
				&  	The letter immediately following \bs l is converted to lowercase. For example, \mono{\bs lEmail} is equivalent to email \br \\
	\bs U		&	All subsequent letters uppercase \\
				&	All the letters immediately following \mono{\bs U} are converted to uppercase until \mono{\bs E} is reached \br \\
	\bs L		&	All subsequent letters lowercase \\
				&	All the letters immediately following \mono{\bs L} are converted to lowercase until \mono{\bs E} is reached \br \\
	\bs Q		&	Disables pattern matching until \bs E \\
				&	This would be covered in the ``Regular Expressions'' chapter. \br \\
	\bs E		& 	Ends \bs U, \bs L, \bs Q \\
				&	Terminates the effect of \bs U, \bs L or \bs Q. \br \\
\hline
\end{tabular}
\end{center}
\caption{The most commonly used escape characters in Perl}\label{tab:escape}
\end{table}

These escape characters are predefined and can be used in double-quoted strings.
There is another case where backslashes have to be used in a string. 
This is known as character escaping. What does that mean? Consider you would 
like to use double quotes in a double-quoted string. For example you would like to print this 
English sentence instead of the ``Hello World'' phrase in Example 2.1: \\

\textit{Howdy says, "Give me \$500".} \\

That is, you try to \mono{print} this sentence by: \\

\begin{verbatimtab}[4]
	print "Howdy says, "Give me $500".";
\end{verbatimtab}
 
If you execute this statement, you will get into trouble, because \mono{"} is used to mark the beginning
and the end of the string literal itself. Perl locates the end of the string by searching forward until the second double quote is found. 
If the literal contains double quotes itself, Perl will not know where the string literal terminates. 
In the example above, Perl will think the string ends after ``Howdy says,~''.
Also, after you have learned variable substitution in the next chapter you will realize that the 
symbol \mono{\$} is used for variable substitution. You have to tell Perl explicitly you would like to use 
the symbol as is instead of performing variable substitution. To get around this problem, just place the 
\bs\ character before the two symbols concerned, and this is what we mean to ``escape'' a character. 
So, the correct way to \mono{print} this sentence using double quotes is: \\

\begin{verbatimtab}[4] 
	print "Howdy says, \"Give me \$500\".";
\end{verbatimtab}
 
However, wise Perl programmers will not do this, as the backslashes make the whole expression ugly. 
If we choose to use single quotes instead, we don't even have to escape anything: \\
 
\begin{verbatimtab}[4] 
   print 'Howdy says, "Give me $500".';
\end{verbatimtab} 

Single-quoted strings do not support variable substitution, so the \mono{\$} needs not be escaped. Also, because 
the symbol \mono{"} does not carry any significance in the string, it does not need to be escaped as well. There are 
only two characters that need to be escaped in single-quoted strings, namely ' and \bs. For double-quoted 
strings, a number of characters have to be escaped, and it would become clear as you work through the chapters 
in this tutorial. \\

Empty strings are denoted by ``'' or `', that is, two quotes with nothing in between. \\

\section{Introduction to Data Structures}

Every programming language has certain kinds of data structures builtin. A data structure can be thought of as a 
virtual container residing in the memory in which data is stored. Each data structure is associated with a 
data type specifying the type of data permitted in the data structure. Data type is important in programming 
languages because data of different types are likely to be treated differently. For example, numbers are 
sorted by numerical value; while strings are sorted in alphabetical order. Many programming languages, like 
C++, Java and Visual Basic, have a large number of data types, e.g. integer, double, string, boolean ... just 
to name a few. They require declaration of a data type when a data structure is created, and this data type 
cannot be changed afterwards. There are both advantages and disadvantages to this approach. As different data 
types occupy different amount of storage in the memory, the underlying machine actually requires some type 
information to convert the high-level programming constructs into assembly instructions in the compilation stage. 
Also, by having the data type fixed there are less ambiguities as to how the data is to be handled. The most 
obvious disadvantage, of course, is that explicit data conversion is necessary in such programming languages. \\

As of Perl 5, Perl officially differentiates only two types of data: scalar data and list data. Moreover, Perl 
does not enforce strict type-checking, instead, it is loosely-typed. This may change in Perl 6, but you will 
not see it in the near future. \\

\term{Scalar data} represents a piece of data. All literals are scalar data. Variables are also scalar data. 
As the underlying machine requires explicit declaration of data types, Perl needs to convert the data between 
different data types as needed in the underlying implementation, while a Perl programmer can be oblivious to 
such data conversion. In the next chapter you would see example code in practice. \\

Another type is \term{list data}. List data is an aggregation of scalar data. Arrays and hashes belong to 
this type. While you may not have a clear picture of how list data look like at this point, you would have a 
clear idea after reading the next chapter. \\

Three basic data structures are provided by Perl, namely scalar variables, arrays and associative arrays 
(hashes). I am going to give an introduction to the three types of data structures at this point, and in the 
next chapter you would see the functions and operations associated with these data structures. \\

A \term{scalar variable}, or simply a variable, is a named entity representing a piece of scalar data of which the content can be modified 
throughout its lifetime. What does this mean? A variable is conceptually like a virtual basket, and only one 
object is allowed in it at any one time. If at some time you would like to place something else in the basket, 
you have to replace the existing object with a new one, and the existing object is discarded. In Perl a 
variable can store a piece of string or number (or a reference, which we haven't come to yet). Unlike other 
programming languages, Perl gives you the flexibility that at one time you may store a number and at other 
times you may store a string in the same variable, however, it is a good practice to always store data of a 
particular type at any time in the same variable to avoid confusion. \\
 
Because the value of a variable can be modified at any point, and there can be many variables that are 
concurrently in use at a time, we have to specify which one to address. Therefore, variables are named, and 
on the other hand literals are unnamed. This name is known as an \term{identifier}. By default, all variables are 
\term{global}, that is, after the variable is first used in the script, it can be referred to at any time, anywhere 
until the script terminates. However, it is a good practice not to use global variables excessively. Instead, 
most variables are actually used for temporary storage only and can be restricted to be valid for a limited 
time. This concerns the lifetime of a variable, and in turn the idea of \term{scope}. This would be discussed 
in Chapter~\ref{chap:scope}. \\

Sometimes we are dealing with a set of related data. Instead of storing them separately in variables, we 
may store them as list data, which is a collection of scalar data sharing a single identifier (name). Arrays
and hashes are two types of list data. \\

An \term{array} is a named entity representing a list of scalar data, with each item assigned an \term{index}. 
In an array, the integral index (or \term{subscript}) uniquely identifies each item in the 
array. The first item has index 0, the one afterwards has index 1, and so on. Each item in an array is a piece of 
scalar data, and, therefore, (in Perl only) numbers as well as strings may coexist in the array. An 
array can be empty, that is, containing no elements (called a \term{null array}). A representation of an example 
array containing some data is shown below, the column on the left is the index and the data is on the right: \\

\begin{table}[htbp]
\begin{center}
\begin{tabular}{cl}
\hline
\textbf{Index}	&	\textbf{Data}	\\
\hline 
	0	&	``Apple'' \br \\
	1	&	36 \br \\
	2	&	``Hello, World'' \br \\
	3	&	``School'' \br \\
\hline
\end{tabular}
\end{center}
\caption{Contents of a sample array in Perl}\label{tab:arraysample}
\end{table}


A \term{hash} is a special data structure. It is similar to an array except that the index is not an integer, 
so the term ``index'' is not customarily used for hashes. Instead, a string is used for indexing, and is known as 
a \term{key}. The key is conceptually like a tag which is attached to the corresponding value. The key and the value forms
a pair (key-value pair). Like an array, the keys in a hash have to be distinct to distinguish a key-value pair
from another. Recall that ordering in arrays is determined by the indices of the items (because we can say the first 
item is the one which has subscript 0, the second item which has subscript 1, and so on). However, in a hash no 
such ordering is present.  You will see in the next chapter that we may ``sort'' the hashes by keys or by values 
for presentation purposes. However, this does not physically reorder the keys or the values in the hash. It merely 
rearranges the key-value pairs for screen output or to be passed to another process for further processing. \\

Hashes (or hash tables in Computer Science parlance) are especially useful in dictionary programs. Assume that 
the program works as follows. It requires a user to enter an English word into the text entry box that is to be 
searched in the dictionary database. Inside the dictionary is actually a long list of key-value pairs, where the 
key is the word entry and the value is an ID that the database uses internally to retrieve the corresponding record 
(containing the explanations, pronunciation etc.). The term entered by the user is queried in the dictionary. If 
the entry matches any key, the corresponding ID is obtained and is used to retrieve the record for the word 
specified; Otherwise, the term is not found and the program returns an error. Hash table is an efficient data 
structure for data storage. A well-implemented hash table requires only several comparisons to retrieve the value 
if the key is in the hash. More surprisingly, even if a given key does not exist in a hash, it is NOT necessary 
to search through all the keys in the hash before returning the key-not-found error. The reason for this concerns 
the principle behind hash tables. You may find more information in Appendix~\ref{chap:advhash} or in any textbooks 
on data structures and algorithms.  \\

Several entries of a possible hash table for the above dictionary program is shown below: \\

%\clearpage	% TODO: manual hack

\begin{table}[!htbp]
\begin{center}
\begin{tabular}{cl}
\hline
\textbf{Key}	&	\textbf{Value}	\\
\hline 
	``Boy''		&	342 \br \\
	``Apple''	&	165 \br \\
	``Kite''	&	1053 \br \\
	...			&	... \br \\
\hline
\end{tabular}
\caption{Contents of a sample hash in Perl}\label{tab:hashsample}
\end{center}
\end{table}


In the next chapter, you will learn how to manipulate the data structures discussed. You will know how to 
construct an array, and remove items from it, etc. \\

\section*{Summary}

\begin{summary}
\item Perl is especially strong in text manipulation and extraction of information.
\item To create a Perl program, you need only a text editor and the perl interpreter.
\item The \mono{-w} switch of the interpreter enables the output of warnings.
\item The \mono{print()} function can be used to output a string to the standard output (screen by default).
\item Interpreter switches are either specified on the command line or in the script itself as a shebang line for Unix systems.
\item Comments are preceded by the \mono{\#} symbol and can be placed anywhere in your program. 
\item A Perl program consists of statements, with each statement terminating with a semicolon.
\item Integers can be expressed in hexadecimal, octal or decimal notation.
\item Double-quoted string literals perform variable substitution, and recognize a number of escape characters.
\item Single-quoted string literals do not perform variable substitution.
\item Scalar data represents a single piece of data. It can be a literal or a scalar variable.
\item List data represents a set of scalar data. Arrays and hashes belong to this type.
\item An array uses a zero-based subscript to refer to the element being referred to, while hash elements are identified by the key.
\end{summary}
