181x Filetype PDF File size 0.32 MB Source: link.springer.com
APPENDIX ■ ■ ■ Perl Basics This appendix contains lightly edited passages from Beginning Perl,Second Editionby James Lee, (Apress, 2004; ISBN: 1-59059-391-X). The goal is to provide you with a refresher course on the basics of Perl. If you’re completely unfamiliar with Perl, I recommend picking up a copy of Beginning Perlto get the most out of this book. Our First Perl Program Assuming that you now have acopy of Perl installed on your machine, you are ready to start using Perl. If not, go back and follow the instructions (in Beginning Perl,Second Edition). The next step is to write our first Perl program. Here’s what it will look like: #!/usr/bin/perl -w print "Hello, world!\ n"; We highly suggest that you type this example in and try to make it work, so before we go any further, a quick note on editors. Perl source code is just plain text and should be written with aplain text editor rather than a word processor. Your operating system, whether Unix or Windows, comes with aselection of text editors. You may have afavorite already, so feel free to use it. If not, may we suggest vi (http://www.vim.org), emacs (http://www.xemacs.org), and nedit (http://www.nedit.org). Windows provides WordPad and Notepad, but they lack many features of modern text editors, so they should be avoided. nedit is the most WordPad- and Notepad-like, so give it a try. The next step is to fire up your editor of choice, type in the code shown previously, and save it into a file named helloworld.pl in the directory we just made. Then, to execute it, type $ perl helloworld.pl Hello, world! $ Congratulations! You’ve successfully written and executed your first Perl program. 283 284 APPENDIX ■ PERL BASICS Keywords Akeywordis aterm in Perl that has apredefined meaning. One example is the term useas we saw in the statement use warnings; Other types of keywords include built-in functions such as print() and control flow constructs such as ifandwhile. We will talk about many built-in functions and control flow constructs in detail as we progress in our discussion of Perl. It’s a good idea to respect keywords and not give anything else the same name as one. For example, alittle later on you’ll learn that you can create and name a variable, and that calling your variable $printis perfectly allowable. The problem with this is that it leads to confusing and uninformative statements like print $print. It is always a good idea to give a variable ameaningful name, one that relates to its content in a logical mannerfor example, $my_name, @telephone_numbers,%account_info, and so on, rather than $a,@b, and %c. Statements and Statement Blocks If functions are the verbs of Perl, then statements are the sentences. Instead of a period, astatement in Perl usually ends with asemicolon, as shown earlier: print "Hello, world!\ n"; To print some more text, we can add another statement: print "Hello, world!\ n"; print "Goodbye, world!\ n"; Escape Sequences UTF8 gives us 65,536 characters, and ASCII gives us 256 characters, but on the average key- board, there’s only a hundred or so keys. Even using the Shift keys, there will still be some characters that you aren’t going to be able to type. There will also be some things that you don’t want to stick in the middle of your program, because they would make it messy or con- fusing. However, you’ll want to refer to some of these characters in strings that you output. Perl provides us with mechanisms called escape sequencesas an alternative way of getting to them. You’ve already seen the use of \ n to start a new line. Table A-1 lists the more common escape sequences. Table A-1. Escape Sequences Escape Sequence Meaning \ t Tab \ n Start a new line (usually called newline) \ r Carriage return \ b Back up one character (backspace) \ a Alarm (rings the system bell) \ x{ 1F18} Unicode character APPENDIX ■ PERL BASICS 285 In the last example in the table, 1F18 is a hexadecimal number referring to a character in the Unicode character set, which runs from 0000-FFFF. As another example, \ x{ 2620}is the Unicode character for askull-and-crossbones! White Space As mentioned previously, white spaceis the name we give to tabs, spaces, and newlines. Perl is very flexible about where you put white space in your program. You’ve already seen that you’re free to use indentation to help show the structure of blocks. You don’t need to use any white space at all, if you don’t want to. If you’d prefer, your programs can all look like this: print"Top level\ n";{ print"2nd level\ n";{ print"3rd level\ n";} print"Where are we?";} This is considered a bad idea. White space is another tool we have to make our programs more understandable; let’s use it as such. Types of Data A lot of programming jargon is about familiar words in an unfamiliar context. You’ve already seen astring, which was aseries of characters. You could also describe that string as a scalar literal constant. What does that mean? By calling a value a scalar, you’re describing the type of data it contains. If you remember your math (and even if you don’t), a scalar is a plain, simple, one-dimensional value. In math, the word is used to distinguish it from a vector, which is expressed as several numbers. Velocity, for example, has apair of coordinates (speed and direction), and so must be avector. In Perl, ascalar is the fundamental, basic unit of data of which there are two kinds: numbers and strings. A literal is value that never changes. The value 5 is a scalar literaland is literally 5; it can never be 4. Perl has three types of scalar literals: integers (such as 5), floating-point numbers (like 3.14159), and strings (for example, “hello, world”). To put it another way, a literal is a con- stantit never changes, as opposed to avariable, which is apiece of memory that can hold ascalar value. Variables are so named because the value stored within them can vary. For instance, $numbercan be assigned 5, and then later can be changed to the value 6. We will talk more about variables later in this appendix. Numbers There are two types of numbers that we’re interested in as Perl programmers: integers and floating-point numbers. The latter we’ll come to in a minute, but let’s work a bit with integers right now. Integers are whole numbers with no numbers after the decimal point, such as 42, –1, or 10. The following program prints a couple of integer literals in Perl: #!/usr/bin/perl -w # number1.pl print 25, -4; 286 APPENDIX ■ PERL BASICS $ perl number1.pl 25-4$ Well, that’s what we see, but it’s not exactly what we want. Fortunately, this is pretty easy to fix. First, we didn’t tell Perl to separate the numbers with a space, and second, we didn’t tell it to put a new line on the end. Let’s change the program so it does that: #!/usr/bin/perl -w # number2.pl print 25, " ", -4, "\ n"; This will do what we were thinking of: $ perl number2.pl 25 -4 $ For the purpose of human readability, we often write large integers such as 10000000 by splitting up the number with commas: 10,000,000. This is sometimes known as chunking. While we might write 10 million with acomma if we wrote acheck for that amount, don’t use the comma to chunk in aPerl program. Instead, use the underscore: 10_000_000. Change the program to look like the following: #!/usr/bin/perl -w # number3.pl print 25_000_000, " ", -4, "\ n"; Notice that those underscores don’t appear in the output: $ perl number3.pl 25000000 –4 $ As well as integers, there’s another type of number: floating-point numbers. These contain everything else, such as 0.5, –0.01333, and 1.1. Note that floating-point numbers are accurate to acertain number of digits. For instance, the number 15.39 may in fact be stored in memory as 15.3899999999999. This is accurate enough for most scientists, so it will have to be for us programmers as well. Here is an example of printing the approximate value of pi: #!/usr/bin/perl -w # number4.pl print "pi is approximately: ", 3.14159, "\ n"; Executing this program produces the following result: $ perl number4.pl pi is approximately: 3.14159 $
no reviews yet
Please Login to review.