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


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 you’ve 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 it’s 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.

Table 1.1: Some of the Perl Special Characters

Character String Does This

\n Newline or line-feed
\r Carriage return
\t Tab
\f Form-feed
\b Backspace
\033 ASCII 27 (Escape) in octal
\x1B Same in hexadecimal
\cD Control-D
\\ Backslash
\" Double quote
\' Single quote
\u Uppercase next character
\U Uppercase following characters
\l, \L Same as above, but lowercase
\E End \U or \L


NOTE:  Table 1.1 doesn’t list all of the Perl special “escaped” characters. These are just the most common. We will cover this subject in detail as you become more adept with Perl.

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, you’ve 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.


Figure 1.7:  The new results of running hello.pl

There’s one more line in the program, however, and we shouldn’t move on without explaining it:

   #              End hello.pl

This is a comment line, as you have learned, but why? Obviously, it’s the end of the program because there’s no more program after it.

Yes, it is quite obvious in a tiny snippet of code such as we’ve typed into hello.pl. However, the programs we will work on as your knowledge increases will be much more complicated, much larger, and won’t 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 Perl

The code we’ve written so far is simple. Let’s make it a bit more complicated—and therefore useful—by introducing three new concepts:

  Variable Data stored in specific memory location
  Scalar A single variable that defines either numeric or string (character) data
  List A number of scalars stored sequentially in one variable

Perl Variables: What’s in a Name

The 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 you’re thinking of skipping this section, please don’t!

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.


WARNING:  The rules of good, structured programming apply to Perl as they do to any other language: make your Perl code readable by using lots of comments. Just because a language allows a fast and loose form of variable declaration is no excuse for writing “spaghetti code.”


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.