Buy this Book
Print Book $34.95 Read it Now!
Print Book £24.95
Add to UK Cart
Reprint Licensing

Learning Perl on Win32 Systems
Learning Perl on Win32 Systems

By Randal L. Schwartz, Erik Olson, Tom Christiansen
Price: $34.95 USD
£24.95 GBP

Cover | Table of Contents | Colophon


Table of Contents

Chapter 1: Introduction
Perl is a language designed for people who need to get things done. Written by the amazing Larry Wall as a kind of glue language to tie together all of the loose ends of everyday computing life, Perl is a tool for leveraging the skills and tools that you already have. Perl has become an indispensable boon to Windows NT webmasters, power users, administrators, and programmers who have discovered how much easier it is to get their work done when Perl is doing some of it for them.
Do you know a little about C? Then you probably already know a lot about Perl. In fact, if you have used any programming or scripting language before, you might be surprised at how familiar Perl looks. Perl is not only an easy language to use, but also makes great use of your existing tools and solutions.
Perl is an easy language, but it's also a rich language, offering lots of functionality right out of the box. You'll be surprised at how much you can do with just a little bit of Perl code. Often, rewriting a small scrap of Perl wizardry requires hundreds of lines of C. Some languages that let you do a lot with a little don't let you do a whole lot. Perl not only lets you do a lot, it lets you do so with minimal effort. And if you run into something that you can't do in Perl, you'll find most likely that Perl will cooperate quite nicely with whatever you do have to do it in.
For many addicts, Perl is more than a language, it's an entire culture. For many folks, Perl was an indispensable part of their UNIX toolkits that they took with them to new environments. As a result, Perl grew, and became even more general and more powerful. What was once just an exceptional text-processing language that bound UNIX programs together has become a widespread language that seems to bind much of the Internet together. Perl is now used to create web pages, read Usenet news, do system adminstration and systems programming, write network clients and servers, and much more.
The three chief virtues of a Perl programmer (indeed, of any programmer) are sometimes said to be laziness, impatience, and hubris. Although these may seem like undesirable qualities at first blush (just ask your SO), there's more to this than there appears to be.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
History of Perl
Perl is a language designed for people who need to get things done. Written by the amazing Larry Wall as a kind of glue language to tie together all of the loose ends of everyday computing life, Perl is a tool for leveraging the skills and tools that you already have. Perl has become an indispensable boon to Windows NT webmasters, power users, administrators, and programmers who have discovered how much easier it is to get their work done when Perl is doing some of it for them.
Do you know a little about C? Then you probably already know a lot about Perl. In fact, if you have used any programming or scripting language before, you might be surprised at how familiar Perl looks. Perl is not only an easy language to use, but also makes great use of your existing tools and solutions.
Perl is an easy language, but it's also a rich language, offering lots of functionality right out of the box. You'll be surprised at how much you can do with just a little bit of Perl code. Often, rewriting a small scrap of Perl wizardry requires hundreds of lines of C. Some languages that let you do a lot with a little don't let you do a whole lot. Perl not only lets you do a lot, it lets you do so with minimal effort. And if you run into something that you can't do in Perl, you'll find most likely that Perl will cooperate quite nicely with whatever you do have to do it in.
For many addicts, Perl is more than a language, it's an entire culture. For many folks, Perl was an indispensable part of their UNIX toolkits that they took with them to new environments. As a result, Perl grew, and became even more general and more powerful. What was once just an exceptional text-processing language that bound UNIX programs together has become a widespread language that seems to bind much of the Internet together. Perl is now used to create web pages, read Usenet news, do system adminstration and systems programming, write network clients and servers, and much more.
The three chief virtues of a Perl programmer (indeed, of any programmer) are sometimes said to be laziness, impatience, and hubris. Although these may seem like undesirable qualities at first blush (just ask your SO), there's more to this than there appears to be.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Purpose of Perl
Well, you've made it through the Perl hype. You might be wondering why you'd ever use Perl. This section provides a couple of ideas.
You can use Perl for World Wide Web (WWW) programming. You've probably heard that Perl has become a sort of lingua franca for the Web (actually, you may have heard that statement for more than one language, but we'll say it again here). Perl cannot only be used as a CGI language (for which there are wonderful modules available), but it can be used as an ISAPI extension (an in-process extension to your web server), or even as an ActiveX scripting language. You can also use Perl to validate HTML syntax, to verify that web hyperlinks are still correct, and to fetch URLs from the Internet.
You can use Perl for many system administration chores. Not only will Perl let you manipulate the Registry, the Event Log, and Windows NT user account information, it's also the best tool going for processing log files of nearly any format.
You can use Perl to drive your favorite word processor or spreadsheet using OLE Automation. You can use the freely available Win32::ODBC module or Active Data Objects (ADO) to access your favorite local or enterprise database.
You can use Perl to retrieve (and filter) your email and Usenet news. You can use Perl to send email, interact with FTP and HTTP servers, and be a client for nearly any other type of Internet server you can dream up.
You can use Perl to process textual or numerical data, to prototype projects, to do quick search and replace functions in text files, to drive the execution of a sequence of commands, and much, much more.
In short, Perl can do zillions of thing to help you do your job faster and get back to doing things that are fun (many of which you can also use Perl to do). And along the way, you might find that the journey itself can be a lot of fun.
Like any language, Perl can be "write only"; it's possible to write programs that are impossible to read. But with proper care, you can avoid this common accusation. Yes, sometimes Perl looks like line noise to the uninitiated, but to the seasoned Perl programmer, it looks like checksummed line noise with a mission in life. If you follow the guidelines of this book, your programs should be easy to read and easy to maintain, but they probably won't win any obfuscated Perl contests.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Availability
Unless you have had the good fortune of having a system administrator install Perl on your workstation, you will need to obtain and install a copy yourself.
Perl is distributed under the GNU Public License, which says something like, "you can distribute binaries of Perl only if you make the source code available at no cost, and if you modify Perl, you have to distribute the source to your modifications as well." And that's essentially free. You can get the source to Perl for the cost of a few megabytes over a wire.
At the time of this writing, there are two Perl distributions that run on Windows NT and Windows 95. There is the ActiveState port of Perl, called Perl for Win32, and starting with Perl 5.004, the standard Perl distribution includes support for Win32 systems. The two versions are largely compatible, with some of the Perl 5.004 code being based on the ActiveState port, but there are some differences. The programs and examples presented in this tutorial have been tested on both systems; when a distribution requires different code, we point that fact out. The architects of both distibutions have announced their intention to merge the distributions, but they have not yet announced a time frame for that to happen.
The canonical source for the ActiveState Perl for Win32 distribution at the time of this writing is at http://www.activestate.com. You can also find the source and binaries for the Perl for Win32 distribution at CPAN. To use the CPAN archives, visit http://www.perl.com/CPAN for a mirror site close to you. The CPAN site will also provide the source distribution for the UNIX version of Perl and precompiled binaries for other platforms. If you're absolutely stumped, write bookquestions@ora.com and say "Where can I get Perl?!?!"
Perl for Win32 comes in a variety of flavors, in both source and binary distributions. Unless you have access to a C++ compiler, you'll probably want to get the binary distribution that contains the Perl executables and libraries, pre-built and ready to use. You might also want to grab the source distribution for reference purposes, if you're familiar with C/C++.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Support
Perl is the child of Larry Wall, and is still being coddled by him. Bug reports and requests for enhancements generally get fixed in later releases, but he is under no obligation to do anything with them. Nevertheless, Larry really does enjoy hearing from all of us, and does truly like to see Perl be useful to the world at large. Direct email generally gets a response (even if it is merely his email answering machine), and sometimes a personal response. These days, Larry is actually acting as an architect to the "Perl 5 Porters" group, a bunch of very clever people that have had a lot to do with the last few Perl releases. If Larry got hit by a bus, everyone would be very sad for a long time, but Perl would still continue to mature under the direction of this group.
You will probably find that your best bet for support comes from the global online Perl community, accessible via the Usenet newsgroup comp.lang.perl.misc. If you are emailable to the Internet, but not amenable to Usenet, you can also wire yourself into this group by sending a request to perl-users-request@cs.orst.edu, which will reach a human who can connect you to a two-way email gateway into the group, and give you guidelines on how the group works.
When you subscribe to the newsgroup, you'll find roughly 50 to 200 postings a day (at the time of this writing) on all manner of subjects from beginner questions to complicated porting issues and interface problems, and even a fairly large program or two.
The newsgroup is almost constantly monitored by many Perl experts. Most of the time, your question gets answered within minutes of your news article reaching a major Usenet hub. Just try getting that level of support from your favorite software vendor for free! Larry himself reads the group as time permits, and has been known to interject authoritative articles to end bickering or clarify a point. After all, without Usenet, there probably wouldn't have been a place to easily announce Perl to the world.
In addition to the newsgroup, you should also be reading the Perl documentation which comes with the Perl distribution. Another authoritative source is the
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Basic Concepts
A Perl program is a bunch of Perl statements and definitions thrown into a file. You can execute the file by invoking the Perl interpreter with the script name as an argument. You will often see a line
#
            !/usr/bin/perl
as the first line of a Perl script. This line is a bit of magic employed by UNIX-like operating systems to automatically execute interpreted languages with the correct command interpreter. This line is called a shebang line due to the first two characters: # is sometimes called sharp, and ! is sometimes called bang. This line normally won't work for Perl-for-Win32 users, although it doesn't hurt anything since Perl sees lines beginning with # as comments.
The invocation examples that follow assume that you have invoked the Windows NT command interpreter (cmd.exe) and are typing into a console window. You can run Perl scripts from the Explorer or the File Manager (assuming that you've associated the script extension with the Perl interpreter) by double-clicking on the script icon to launch it. Throughout this book, we're going to be discussing standard output and input streams; these are generally assumed to be your console window.
We recommend naming scripts with a .plx extension. Traditionally, Perl modules have a .pm extension, and Perl libraries have a .pl extension. The ActiveState installer prompts you to associate .pl with the interpreter.
You can always execute a script by calling the Perl interpreter with the script as an argument:
> perl myscript.plx
         
You can also associate files with the .plx extension (or another of your choosing) with the Perl interpreter, so that executing
> myscript.plx
         
will correctly invoke the Perl interpreter and execute your script. This step is normally done for you by the ActiveState installation script for the .pl extension, but if you wish to change the extension or if you've got the standard distribution, you can do this step manually. If you're using Windows NT 4.0 (or greater), the following commands will do the trick (use the full path to your interpreter):
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
A Stroll Through Perl
We begin our journey through Perl by taking a little stroll. This stroll presents a number of different features by hacking on a small application. The explanations here are extremely brief—each subject area is discussed in much greater detail later in this book. But this little stroll should give you a quick taste for the language, and you can decide if you really want to finish this book instead of reading some more Usenet news or running off to the ski slopes.
Let's look at a little program that actually does something. Here is your basic "Hello, world" program (use any text editor to type it in):
               print ("Hello, world!\n");
This single line is the entire program. The built-in function print starts it off, and in this case has just one argument, a C-like text string. Within this string, the character combination \n stands for a newline character, just as it does in C. The print statement is terminated by a semicolon (;). As in Pascal or C, all simple statements in Perl are terminated by a semicolon.
When you invoke this program, the Perl interpreter parses the entire program and then executes the compiled form. The first and only operation is the execution of the print function, which sends any arguments to the standard output. After the program has completed, the Perl process exits, returning a successful exit code to the parent process.
Soon, you'll see Perl programs in which print and other functions are sometimes called with parentheses, and sometimes called without them. The rule is simple: in Perl, parentheses for built-in functions are never required nor forbidden. Their use can help or hinder clarity, so use your own judgment.
Let's add a bit more sophistication. The Hello, world greeting is a touch cold and inflexible. Let's have the program call you by your name. To do this, we need a place to hold the name, a way to ask for the name, and a way to get a response.
One kind of place to hold values (like a name) is a
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Exercises
Normally, each chapter will end with some exercises, for which answers will be found in Appendix A. For this stroll, the answers have already been given above.
  1. Type in the example programs, and get them to work. (You'll need to create the secret word lists as well.) Consult your local Perl guru if you need assistance.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Chapter 2: Scalar Data
A scalar is the simplest kind of data that Perl manipulates. A scalar is either a number (like 4 or 3.25e20) or a string of characters (like "hello" or the Gettysburg Address). Although you may think of numbers and strings as very different things, Perl uses them nearly interchangeably, so we'll describe them together.
A scalar value can be acted upon with operators (like plus or concatenate), generally yielding a scalar result. A scalar value can be stored into a scalar variable. Scalars can be read from files and devices and written out as well.
Although a scalar is either a number or a string, consider numbers and strings separately for the moment. Numbers first, strings in a minute...
As you'll see in the next few paragraphs, you can specify both integers (whole numbers, like 17 or 342) and floating-point numbers (real numbers with decimal points, like 3.14, or 1.35 times 1025). But internally, Perl computes only with double-precision floating-point values. This means that there are no integer values internal to Perl; an integer constant in the program is treated as the equivalent floating-point value. You probably won't notice the conversion (or care much), but you should stop looking for integer operations (as opposed to floating-point operations), because there aren't any.
A literal is the way a value is represented in the text of the Perl program. You could also call this a constant in your program, but we'll use the term literal. Literals are the way in which data is represented in the source code of your program as input to the Perl compiler. (Data that is read from or written to files is treated similarly, but not identically.)
Perl accepts the complete set of floating-point literals available to C programmers. Numbers with and without decimal points are allowed (including an optional plus or minus prefix), as well as tacking on a power-of-10 indicator ( exponential notation) with
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
What Is Scalar Data?
A scalar is the simplest kind of data that Perl manipulates. A scalar is either a number (like 4 or 3.25e20) or a string of characters (like "hello" or the Gettysburg Address). Although you may think of numbers and strings as very different things, Perl uses them nearly interchangeably, so we'll describe them together.
A scalar value can be acted upon with operators (like plus or concatenate), generally yielding a scalar result. A scalar value can be stored into a scalar variable. Scalars can be read from files and devices and written out as well.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Numbers
Although a scalar is either a number or a string, consider numbers and strings separately for the moment. Numbers first, strings in a minute...
As you'll see in the next few paragraphs, you can specify both integers (whole numbers, like 17 or 342) and floating-point numbers (real numbers with decimal points, like 3.14, or 1.35 times 1025). But internally, Perl computes only with double-precision floating-point values. This means that there are no integer values internal to Perl; an integer constant in the program is treated as the equivalent floating-point value. You probably won't notice the conversion (or care much), but you should stop looking for integer operations (as opposed to floating-point operations), because there aren't any.
A literal is the way a value is represented in the text of the Perl program. You could also call this a constant in your program, but we'll use the term literal. Literals are the way in which data is represented in the source code of your program as input to the Perl compiler. (Data that is read from or written to files is treated similarly, but not identically.)
Perl accepts the complete set of floating-point literals available to C programmers. Numbers with and without decimal points are allowed (including an optional plus or minus prefix), as well as tacking on a power-of-10 indicator ( exponential notation) with E notation (that's e or E). For example:
1.25      # about 1 and a quarter
7.25e45   # 7.25 times 10 to the 45th power (a big number)
-6.5e24   # negative 6.5 times 10 to the 24th
          # (a "big" negative number)
-12e-24   # negative 12 times 10 to the -24th
          # (a very small negative number)
-1.2E-23  # another way to say that
Integer literals are also straightforward, as in:
12
15
-2004
3485
Don't start the number with a 0, because Perl supports octal and hexadecimal (hex) literals. Octal numbers start with a leading 0, and hex numbers start with a leading
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Strings
Strings are sequences of characters (like hello). Each character is an 8-bit value from the entire 256-character set (there's nothing special about the NUL character, as in some languages).
The shortest possible string has no characters. The longest string fills all of your available memory (although you wouldn't be able to do much with that). This is in accordance with the principle of "no built-in limits" that Perl follows at every opportunity. Typical strings are printable sequences of letters, digits, and punctuation in the ASCII 32 to ASCII 126 range. However, the ability to have any character from to 255 in a string means that you can create, scan, and manipulate raw binary data as strings—a task with which most other utilities would have great difficulty. (For example, you can patch your operating system by reading it into a Perl string, making the change, and writing the result back out.)
Like numbers, strings have a literal representation (the way you represent the string in a Perl program). Literal strings come in two different flavors: single-quoted strings and double-quoted strings. Another form that looks rather like these two is the back-quoted string (`like this`). This form isn't so much a literal string as a way to run external commands and get back their output. This form is covered in Chapter 14.
A single-quoted string is a sequence of characters enclosed in single quotes. The single quotes are not part of the string itself; they're just there to let Perl identify the beginning and the ending of the string. Any character between the quote marks (including newline characters, if the string continues onto successive lines) is legal inside a string. There are two exceptions: to get a single quote into a single-quoted string, precede it by a backslash; and, to get a backslash into a single-quoted string, precede the backslash by a backslash. In other pictures:
'hello'     # five characters: h, e, l, l, o
'don\'t'    # five characters: d, o, n, single quote, t
''          # the null string (no characters)
'silly\\me' # silly, followed by backslash, followed by me
'hello\n'   # hello followed by backslash followed by n
'hello
there'      # hello, newline, there (11 characters in all)
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Scalar Operators
An operator produces a new value (the result) from one or more other values (the operands). For example, + is an operator because it takes two numbers (the operands, like 5 and 6), and produces a new value (11, the result).
Perl's operators and expressions are generally a superset of those provided in most other ALGOL/Pascal-like programming languages, such as C or Java. An operator expects either numeric or string operands (or possibly a combination of both). If you provide a string operand where a number is expected, or vice versa, Perl automatically converts the operand using fairly intuitive rules, which will be detailed in Section 2.4.4 later in this chapter.
Perl provides the typical ordinary addition, subtraction, multiplication, and division operators, and so on. For example:
2 + 3      # 2 plus 3, or 5
5.1 - 2.4  # 5.1 minus 2.4, or approximately 2.7
3 * 12     # 3 times 12 = 36
10.2 / 0.3 # 10.2 divided by 0.3, or approximately 34
10 / 3     # always floating point divide, so approximately 3.333333...
Additionally, Perl provides the FORTRAN-like exponentiation operator, which many have yearned for in Pascal and C. The operator is represented by the double asterisk, such as 2**3, which is 2 to the power of 3, or 8. (If the result cannot fit into a double-precision floating-point number, such as a negative number to a noninteger exponent, or a large number to a large exponent, you'll get a fatal error.)
Perl also supports a modulus operator. The value of the expression 10 % 3 is the remainder when 10 is divided by 3, which is 1. Both values are first reduced to their integer values, so 10.5 % 3.2 is computed as 10 % 3.
The logical comparison operators are <, <=, ==, >=, >, and !=. These operators compare two values numerically, returning a true or false value. For example, 3 > 2
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Scalar Variables
A variable is a name for a container that holds one or more values. The name of the variable is constant throughout the program, but the value or values contained in that variable typically change over and over again throughout the execution of the program.
A scalar variable holds a single scalar value (representing a number, a string, or a reference). Scalar variable names begin with a dollar sign followed by a letter, and then possibly more letters, or digits, or underscores. Upper- and lowercase letters are distinct: the variable $A is a different variable from $a. And all of the letters, digits, and underscores are significant, so:
$a_very_long_variable_that_ends_in_1
is different from:
$a_very_long_variable_that_ends_in_2
You should generally select variable names that mean something regarding the value of the variable. For example, $xyz123 is probably not very descriptive, but $line_length is.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Scalar Operators and Functions
The most common operation on a scalar variable is assignment , which is the way to give a value to a variable. The Perl assignment operator is the equal sign (as in C or FORTRAN), which takes a variable name on the left side and gives it the value of the expression on the right, like so:
$a = 17;     # give $a the value of 17
$b = $a + 3; # give $b the current value of $a plus 3 (20)
$b = $b * 2; # give $b the value of $b multiplied by 2 (40)
Notice that the last line uses the $b variable twice: once to get its value (on the right side of the =), and once to define where to put the computed expression (on the left side of the =). This is legal, safe, and in fact, rather common. In fact, the practice is so common that we'll see in a minute that we can write this expression using a convenient shorthand.
You may have noticed that scalar variables are always specified with the leading $. In batch files, Java, or C, you don't need the $ at all. If you bounce back and forth a lot, you'll find yourself typing the wrong things occasionally. This is expected. (Our solution was to stop writing batch files and C programs, but that may not work for you.)
You may use a scalar assignment as a value as well as an operation, as in C. In other words, $a=3 has a value, just as $a+3 has a value. The value is the value assigned, so the value of $a=3 is 3. Although this usage may seem odd at first glance, using an assignment as a value is useful if you wish to assign an intermediate value in an expression to a variable, or if you simply wish to copy the same value to more than one variable. For example:
$b = 4 + ($a = 3);      # assign 3 to $a, then add 4 to that
                        # resulting in $b getting 7
$d = ($c = 5);          # copy 5 into $c, and then also into $d
$d = $c = 5;            # the same thing without parentheses
That last example works because assignment is right-associative.
Expressions like $a = $a + 5
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
<STDIN> as a Scalar Value
At this point, if you're a typical code hacker, you're probably wondering how to get a value into a Perl program. Here's the simplest way. Each time you use <STDIN> in a place where a scalar value is expected, Perl reads the next complete text line from standard input (up to the first newline), and uses that string as the value of <STDIN>. Standard input can mean many things, but unless you do something odd, it means the command console that invoked your program. If there's nothing waiting to be read (typically the case, unless you type ahead a complete line), the Perl program will stop and wait for you to enter some characters followed by a newline (return).
The string value of <STDIN> typically has a newline on the end of it. Most often, you'll want to get rid of that newline right away (there's a big difference between hello and hello\n). It is at this point that our friend, the chomp() function, comes to the rescue. A typical input sequence goes something like this:
$a = <STDIN>; # get the text
chomp($a);    # get rid of that pesky newline
A common abbreviation for these two lines is:
chomp($a = <STDIN>);
The assignment inside the parentheses continues to refer to $a, even after it has been given a value with <STDIN>. Thus, the chomp() function is working on $a. (This is true in general about the assignment operator—an assignment expression can be used wherever a variable is needed, and the actions refer to the variable on the left side of the equal sign.)
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Output with print
So, we get things in with <STDIN>. How do we get things out? With the print() function. This function takes the values within its parentheses and puts them out without any embellishment onto standard output. Once again, unless you've done something odd, this will be your command console. For example:
print("hello world\n"); # say hello world, followed by newline
print "hello world\n";  # same thing
Note that the second example shows the form of print() without parentheses. In fact, many of the operators that look like functions also have a syntactic form that works without the parentheses. Whether or not to use the parentheses is mostly a matter of style and typing agility, although there are a few cases where you'll need the parentheses to remove ambiguity.
We'll see that you can actually give print a list of values, in Section 6.3.1 in Chapter 6, but we haven't talked about lists yet, so we'll put that discussion off until later.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
The Undefined Value
What happens if you use a scalar variable before you give it a value? Nothing serious, and definitely nothing fatal. Variables have the undef value before they are first assigned. This value looks like a zero when used as a number, or the zero-length empty string when used as a string. You will get a warning when running under Perl's -w switch, though, which is a good way to catch programming errors.
Many operators return undef when the arguments are out of range or don't make sense. If you don't do anything special, you'll get a zero or a null string without major consequences. In practice, this scenario is hardly a problem.
One operation we've seen that returns undef under certain circumstances is <STDIN> . Normally, this returns the next line that was read; however, if there are no more lines to read, (such as when you type CTRL-Z at the terminal, or when a file has no more data), <STDIN> returns undef as a value. In Chapter 6, we'll see how to test for this and take special action when there is no more data available to read.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Exercises
See Appendix A for answers.
  1. Write a program that computes the circumference of a circle with a radius of 12.5. The circumference is 2π times the radius, or about 2 times 3.141592654.
  2. Modify the program from the previous exercise to prompt for and accept a radius from the person running the program.
  3. Write a program that prompts for and reads two numbers, and then prints out the result of the two numbers multiplied together.
  4. Write a program that reads a string and a number, and then prints the string the number of times indicated by the number on separate lines. (Hint: use the x operator.)
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Chapter 3: Arrays and List Data
A list is an ordered set of scalar data. An array is a variable that holds a list. Each element of the array is a separate scalar variable with an independent scalar value. These values are ordered—that is, they have a particular sequence from the lowest to the highest element.
Arrays can have any number of elements. The smallest array has no elements, while the largest array can fill all of available memory. Once again, this flexibility is in keeping with Perl's philosophy of "no unnecessary limits."
A list literal (the way you represent the value of a list within your program) consists of comma-separated values enclosed in parentheses. These values form the elements of the list. For example:
(1,2,3)      # array of three values 1, 2, and 3
("fred",4.5) # two values, "fred" and 4.5
The elements of a list are not necessarily constants—they can be expressions that will be evaluated newly each time the literal is used. For example:
($a, 17)      # two values: the current value of $a, and 17
($b+$c,$d+$e) # two values
The empty list (one of no elements) is represented by an empty pair of parentheses:
() # the empty list (zero elements)
An item of the list literal can include the list constructor function, indicated by two scalar values separated by two consecutive periods. This function creates a list of values starting at the left scalar value and continuing up through the right scalar value, incrementing by one at each value. For example:
(1..5)       # same as (1, 2, 3, 4, 5)
(1.2..5.2)   # same as (1, 2, 3, 4, 5)
(2..6,10,12) # same as (2,3,4,5,6,10,12)
($a..$b)     # range determined by current values of $a and $b
Having the right scalar less than the left scalar results in an empty list; you can't count down by switching the order of the values. If the final value is not a whole number of steps above the initial value, the list stops just before the next value would have been outside the range:
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
What Is a List or Array?
A list is an ordered set of scalar data. An array is a variable that holds a list. Each element of the array is a separate scalar variable with an independent scalar value. These values are ordered—that is, they have a particular sequence from the lowest to the highest element.
Arrays can have any number of elements. The smallest array has no elements, while the largest array can fill all of available memory. Once again, this flexibility is in keeping with Perl's philosophy of "no unnecessary limits."
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Literal Representation
A list literal (the way you represent the value of a list within your program) consists of comma-separated values enclosed in parentheses. These values form the elements of the list. For example:
(1,2,3)      # array of three values 1, 2, and 3
("fred",4.5) # two values, "fred" and 4.5
The elements of a list are not necessarily constants—they can be expressions that will be evaluated newly each time the literal is used. For example:
($a, 17)      # two values: the current value of $a, and 17
($b+$c,$d+$e) # two values
The empty list (one of no elements) is represented by an empty pair of parentheses:
() # the empty list (zero elements)
An item of the list literal can include the list constructor function, indicated by two scalar values separated by two consecutive periods. This function creates a list of values starting at the left scalar value and continuing up through the right scalar value, incrementing by one at each value. For example:
(1..5)       # same as (1, 2, 3, 4, 5)
(1.2..5.2)   # same as (1, 2, 3, 4, 5)
(2..6,10,12) # same as (2,3,4,5,6,10,12)
($a..$b)     # range determined by current values of $a and $b
Having the right scalar less than the left scalar results in an empty list; you can't count down by switching the order of the values. If the final value is not a whole number of steps above the initial value, the list stops just before the next value would have been outside the range:
(1.3..6.1) # same as (1, 2, 3, 4, 5)
List literals with lots of short text strings start to look pretty noisy with all the quotes and commas:
@a = ("fred","barney","betty","wilma"); # ugh!
Fortunately, Perl has a shortcut: the "quote-word" syntax, which creates a list from the nonwhitespace parts between the parentheses:
@a = qw(fred barney betty wilma); # better!
@a = qw(
		fred
		barney
		betty
		wilma
);                                # same thing
One use of a list literal is as arguments to the
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Variables
An array variable holds a single list value (zero or more scalar values). Array variable names are similar to scalar variable names, differing only in the initial character, which is an at sign (@) rather than a dollar sign ($). For example:
@fred # the array variable @fred
@A_Very_Long_Array_Variable_Name
@A_Very_Long_Array_Variable_Name_that_is_different
Note that the array variable @fred is unrelated to the scalar variable $fred. Perl maintains separate namespaces for different types of things.
The value of an array variable that has not yet been assigned is (), the empty list.
An expression can refer to array variables as a whole, or it can examine and modify individual elements of the array.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Array Operators and Functions
Array functions and operators act on entire arrays. Some return a list, which can then either be used as a value for another array function, or assigned into an array variable.
Probably the most important array operator is the array assignment operator, which gives an array variable a value. It is an equal sign, just like the scalar assignment operator. Perl determines whether the assignment is a scalar assignment or an array assignment by noticing whether the assignment is to a scalar or an array variable. For example:
@fred = (1,2,3); # The fred array gets a three-element literal
@barney = @fred; # now that is copied to @barney
If you assign a scalar value to an array variable, the scalar value becomes the single element of an array:
@huh = 1; # 1 is promoted to the list (1) automatically
          # that is, @huh now is (1)
An array variable name may appear in a list-literal list. When the value of the list is computed, Perl replaces the array variable name with the current values of the array, like so:
@fred = qw(one two);
@barney = (4,5,@fred,6,7);  # @barney becomes 
                            # (4,5,"one","two",6,7)
@barney = (8,@barney);      # puts 8 in front of @barney
@barney = (@barney,"last"); # and a "last" at the end
# @barney is now (8,4,5,"one","two",6,7,"last")
Note that the inserted array elements are at the same level as the rest of the literals: a list cannot contain another list as an element.
If a list literal contains only variable references (not expressions), you can treat the list literal as a variable. In other words, you can use it on the left side of an assignment. Each scalar variable in the list literal takes on the corresponding value from the list on the right side of the assignment. For example:
($a,$b,$c) = (1,2,3);     # give 1 to $a, 2 to $b, 3 to $c
($a,$b) = ($b,$a);        # swap $a and $b
($d,@fred) = ($a,$b,$c);  # give $a to $d, and ($b,$c) to @fred
($e,@fred) = @fred;       # remove the first element of @fred to $e
                          # this makes @fred = ($c) and $e = $b
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Scalar and List Context
As you can see, each operator is designed to operate on some specified combination of scalars or lists, and returns either a scalar or a list. If an operator or function expects an operand to be a scalar, we say that the operand or argument is being evaluated in a scalar context. Similarly, if an operand or argument is expected to be a list value, we say that it is being evaluated in a list context.
Normally, the context is fairly insignificant. But, sometimes you get a completely different operation depending on whether you are within a scalar or a list context. For example, @fred returns the contents of the @fred array in a list context, but the length of the same array in a scalar context. These subtleties are mentioned when each operator and function is described.
A scalar value used within an array context is promoted to a single-element array.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
<STDIN> as an Array
One previously seen operation that returns a different value in a list context is <STDIN> . As described earlier, <STDIN> returns the next line of input in a scalar context. However, in a list context, it returns all of the remaining lines up to the end-of-file. Each line is returned as a separate element of the list. For example:
@a = <STDIN>; # read standard input in a list context
If the person running the program types three lines, then hits CTRL-Z (to indicate end-of-file), the array ends up with three elements. Each element will be a string that ends in a newline, corresponding to the three newline-terminated lines entered.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Variable Interpolation of Arrays
Like scalars, array values may be interpolated into a double-quoted string. A single element of an array will be replaced by its value, like so:
@fred = ("hello","dolly");
$y = 2;
$x = "This is $fred[1]'s place";    # "This is dolly's place"
$x = "This is $fred[$y-1]'s place"; # same thing
Note that the index expression is evaluated as an ordinary expression, as if it were outside a string. It is not variable interpolated first.
If you want to follow a simple scalar variable reference with a literal left square bracket, you need to delimit the square bracket so it isn't considered part of the array, as follows:
@fred = ("hello","dolly");  # give value to @fred for testing
$fred = "right";
# we are trying to say "this is right[1]"
$x = "this is $fred[1]";    # wrong, gives "this is dolly"
$x = "this is ${fred}[1]";  # right (protected by braces)
$x = "this is $fred"."[1]"; # right (different string)
$x = "this is $fred\[1]";   # right (backslash hides it)
Similarly, a list of values from an array variable can be interpolated. The simplest interpolation is an entire array, indicated by giving the array name (including its leading @ character). In this case, the elements are interpolated in sequence with a space character between them, as in:
@fred = ("a","bb","ccc",1,2,3);
$all = "Now for @fred here!";
            # $all gets "Now for a bb ccc 1 2 3 here!"
You can also select a portion of an array with a slice:
@fred = ("a","bb","ccc",1,2,3);
$all = "Now for @fred[2,3] here!";
            # $all gets "Now for ccc 1 here!"
$all = "Now for @fred[@fred[4,5]] here!"; # same thing
Once again, you can use any of the quoting mechanisms described earlier if you want to follow an array name reference with a literal left bracket rather than an indexing expression.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Exercises
See Appendix A for answers.
  1. Write a program that reads a list of strings on separate lines and prints out the list in reverse order. If you're reading the list from the console, you'll probably need to delimit the end of the list by pressing CTRL-Z.
  2. Write a program that reads a number and then a list of strings (all on separate lines), and then prints one of the lines from the list as selected by the number.
  3. Write a program that reads a list of strings and then selects and prints a random string from the list. To select a random element of @somearray, put
    srand;
    at the beginning of your program (this initializes the random number generator), and then use
    rand(@somearray)
                   
    where you need a random value between and 1 less than the length of @somearray.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Chapter 4: Control Structures
Content preview·Buy PDF of this chapter|