![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Perl CGl Programming: No experience required.
The Strange \n Hello World! is easy enough to figure out, but what is this \n? C-language programmers and others who are, by necessity, familiar with UNIX conventions know this as the newline character. If youve never seen this before, remember carefully the backslash (\) that precedes the n. This is called an escape character because it gives a special meaning to the character that follows it. The \n specifically refers to the line-feed character, with a value of 10 in the ASCII character set. The line-feed is the standard line ender in UNIX; the MS-DOS convention, which has been retained by Windows 95 and NT, is to end each line with a carriage return and a line feed, which in a Perl print command would be set up as \r\n. However, the Perl interpreter knows what operating system its running on and it makes certain allowances for these differences. For now, whether you compose your code on UNIX or Windows NT, you can use the simple \n as a line ender. Table 1.1 lists some other Perl escaped characters.
The escaped double quote (\) can be somewhat confusing. It is used when you want to actually use the double quote character in a string, rather than using it to delimit the string. For example, the following Perl code: print "Hello World!", "\n"; print "\"Hello World\"", "\n"; would result in the following output to the screen: Hello, World! "Hello, World!" Perl also allows a construct to keep you from loading up your strings with backslashes. You may use q/STRING/ and qq/STRING/ too, where STRING is the phrase enclosed between the slashes. Goodbye to Hello We have done just about all we can with this first version of Hello World! You should now be familiar with Perl comment lines, with emphasis on the important first line, which actually is an instruction. Additionally, youve gained a passing acquaintance with the workhorse print function and some of the things that you can do with it. The results of running the new version of the script are illustrated in Figure 1.7.
Theres one more line in the program, however, and we shouldnt move on without explaining it: # End hello.pl This is a comment line, as you have learned, but why? Obviously, its the end of the program because theres no more program after it. Yes, it is quite obvious in a tiny snippet of code such as weve typed into hello.pl. However, the programs we will work on as your knowledge increases will be much more complicated, much larger, and wont be as clear on where one subroutine starts or another ends. It is simply good programming practice to document your code well, not just for others but for your own benefit. And good documentation starts with clearly marking the beginning and end of important sections of code. Variables, Scalars, and Lists in PerlThe code weve written so far is simple. Lets make it a bit more complicatedand therefore usefulby introducing three new concepts:
Perl Variables: Whats in a NameThe capability to store data in locations that have specific names lies at the heart of any useful programming language. Moving data to a specific spot in memory and being able to recall them by name (or location) at a later time is known as working with variables. Perl is no different in this respect. If you have done any programming at all, you will be familiar with the concept of variables. However, the conventions used in Perl can be a little weird for the uninitiated, so if youre thinking of skipping this section, please dont! Storing data in a variable is as straightforward as picking a name and setting it equal to a value. Complex programming languages such as C have lots of complex rules for what types of data can be stored where; for example, in C integers have to go into int variables and strings of characters are stored as char arrays, and so forth. Variables have to be declared and given types before they can be used. Perl, despite all that it owes to C, plays very fast and loose with those rules. In Perl, you declare a variable merely by using it, which helps to make the Perl development process somewhat quicker and easier than programming in C.
|
![]() |
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. |