![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Perl CGl Programming: No experience required.
Introducing ScalarsThe 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 its pretty good at telling the difference between the two and acting properly.
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.
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. Youll see this on your screen (See Figure 1.8): Hello, World for the 2nd time!
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 weve 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 Perls 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 ListsYou 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, wouldnt 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.
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 doesnt care about type at all. Any old thing can go into a liststrings, numbers, characters, anythingand they all happily coexist. Whats 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 isnt 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.
|
![]() |
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. |