Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Perl CGl Programming: No experience required.
(Publisher: Sybex, Inc.)
Author(s): Erik Strom
ISBN: 0782121578
Publication Date: 11/01/97

Bookmark It

Search this book:
 
Previous Table of Contents Next


Introducing Scalars

The most fundamental data in Perl are called scalars. The word can be intimidating to beginners because its meaning is not immediately apparent. A scalar is nothing more than a single piece of data. Scalars differ from another fundamental Perl data-type, which is the list (defined in the Perl Lists section).

Perl regards numeric and string data as scalar values and in most cases it’s pretty good at telling the difference between the two and acting properly.


NOTE:  “Strings” in most programming languages are simply strings of characters. “Now is the time for all good folks to come to the aid of their party” is a string. Notice that it is enclosed in quotes. This is important in Perl.

The important thing to remember about scalar variables is that they always begin with a dollar sign ($). You can call them anything you want, just never forget the dollar sign.


WARNING:  Perl is a case-sensitive language, which means that it distinguishes between upper- and lowercase letters in names. Thus, it will regard $VariableName and $variablename as two different scalar variables.

We can create a second version of hello.pl to illustrate the concept of storing data in scalar variables. Type the following lines into your text editor and save the file as hello2.pl:

   #!/usr/bin/perl

   # hello2, a slightly more sophisticated "Hello World"

        $Hello = "Hello, World";     # String variable
        $TimeAround = 2;             # Numeric variable

        print $Hello, " for the ", $TimeAround, "nd time!", "\n";

   #     End hello2.pl

Now run the program as we did the one we created earlier. You’ll see this on your screen (See Figure 1.8):

   Hello, World for the 2nd time!


Figure 1.8:  Using variables in your Perl script

Notice that you were able to set the two variables, $Hello and $TimeAround, to two entirely unrelated types. Yet the print function knew precisely what to do with them and assembled the resulting output string flawlessly. print is even smarter than we’ve made it appear here; the line could have been written to include the variables in one long string argument, such as the following:

   print "$Hello for the ${TimeAround}nd time!";

The important thing to note here is that TimeAround was enclosed in curly braces to set it off from the nd. But you can see that print has no trouble culling the variables from the other parts of the string and behaving properly.

This “shorthand” capability is one of Perl’s great strengths, as you will see when we begin to do more complicated programs. However, brevity in code is not necessarily an ideal to strive for, unless it directly leads to more efficient code. Writing a program that is clear and understandable is much more important.

Perl Lists

You have learned so far that scalar variables handle and store individual pieces of data. But what if you have a collection of related data? It would be convenient to store all of them in a variable, wouldn’t it?

Perl lists are intended to do just that. Lists are similar to arrays in many other programming languages, where the variable name defines a starting point, index 0, and the members are stored consecutively. You just increase the index and add it to the starting point to arrive at the array member you want.


NOTE:  A Perl list is the equivalent of an array in Visual Basic, C++, and many other languages. The terms will be used interchangeably in this book.

The C language requires that all members of the array are of the same type, which really only means that they are all the same size. Perl doesn’t care about type at all. Any old thing can go into a list—strings, numbers, characters, anything—and they all happily coexist.

What’s in a List?

List notation in Perl is as specific as scalar notation. List names begin with the @ character; after that, you can call them anything you want.

Setting a list equal to something, or loading it with data, is a bit more complex, but we can make it understandable with a few examples.

An array of numbers would be set up like this:

   @Numbers = (1, 2, 3, 4, 5, 6);

We now have an array of six consecutive numbers called @Numbers. In Perl, as in many other languages, arrays start at position 0, so if we were to set a scalar variable to the value of the first member of @Numbers:

   $OneNumber = $Numbers[0];

$OneNumber would be equal to 1.

Notice that the notation changed a little in the last line: We referred to the first element of @Numbers with a dollar sign in front of it. But isn’t that how we note a scalar value?

Yes, it is. And the notation is correct because just one member of a list is a scalar, so you must use the dollar sign in front of it. The subscript, which is the part of $Numbers[0] enclosed in brackets, is where you tell Perl which member of the array you want.

Streamlined Perl…

Here’s a handy Perl shortcut. Because the members of the array are consecutive numbers, you could have initialized it like this:

   @Numbers = (1..6);

It’s the same as specifying each of the numbers from 1 to 6, as far as Perl is concerned.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.