对匿名哈希的创建与使用 #!/usr/bin/perl use warnings; use strict; 普通方法定义hash引用 my %gilligan_info = ( name => 'Gilligan', hat => 'White', shirt => 'Red', position => 'First Mate', ); my %shipper_info = (
system LIST system PROGRAM LIST Does exactly the same thing as exec LIST , except that a fork is done first, and the parent process waits for the child process to complete. Note that argument processi
You probably have some questions about Perl, and maybe some about this book, especially if you've already flipped through the book to see what's coming. So, we'll use this chapter to answer them. 1.1.
1.2. What Does "Perl" Stand For? Perl is sometimes called the "Practical Extraction and Report Language" though it has been called a "Pathologically Eclectic Rubbish Lister" among other expansions.
1.3. How Can I Get Perl? You probably already have it. At least, we find Perl wherever we go. It ships with many systems, and system administrators often install it on every machine at their site. I
1.4. How Do I Make a Perl Program? It's about time you asked (even if you didn't). Perl programs are text files; you can create and edit them with your favorite text editor. (You don't need any spec
1.6. Exercises Normally, each chapter will end with some exercises, with the answers in Appendix A. But you don't need to write the programs needed to complete this section as they are supplied with
1.5. A Whirlwind Tour of Perl So, you want to see a real Perl program with some meat? (If you don't, just play along for now.) Here you are: #!/usr/bin/perl
@lines = `perldoc -u -f atan2`;
2.1. Numbers Though a scalar is most often either a number or a string, it's useful to look at numbers and strings separately for the moment. We'll cover numbers first and then move on to strings. 2
2.2. Strings Strings are sequences of characters (like hello). Strings may contain any combination of any characters.[] The shortest possible string has no characters. The longest string fills all o
2.4. Scalar Variables A variable is a name for a container that holds one or more values.[*] The name of the variable stays the same throughout the program, but the value or values contained in that
2.3. Perl's Built-in Warnings Perl can be told to warn you when it sees something suspicious going on in your program. To run your program with warnings turned on, use the -w option on the command l
2.5. Output with print It's generally a good idea to have your program produce some output; otherwise, someone may think it didn't do anything. The print( ) operator makes this possible. It takes
2.6. The if Control Structure Once you can compare two values, you'll probably want your program to make decisions based upon that comparison. Like all similar languages, Perl has an if control stru
2.7. Getting User Input At this point, you're probably wondering how to get a value from the keyboard into a Perl program. Here's the simplest way: use the line-input operator, <STDIN>.[] [] This is
2.8. The chomp Operator The first time you read about the chomp operator, it seems overspecialized. It works on a variable, and the variable has to hold a string. If the string ends in a newline cha
2.10. The undef Value What happens if you use a scalar variable before you give it a value? Nothing serious and definitely nothing fatal. Variables have the special undef value before they are first
2.9. The while Control Structure Like most algorithmic programming languages, Perl has a number of looping structures.[] The while loop repeats a block of code as long as a condition is true: [] Eve
2.12. Exercises See Appendix A for answers to the following exercises: [5] Write a program that computes the circumference of a circle with a radius of 12.5. Circumference is 2p times the radius (ap
2.11. The defined Function One operator that can return undef is the line-input operator, <STDIN>. Normally, it returns a line of text. But if there is no more input, such as at end-of-file, it will
3.2. Special Array Indices If you store in an array an element that is beyond the end of the array, the array is automatically extended as needed. There's no limit on its length as long as there's a
3.1. Accessing Elements of an Array If you've used arrays in another language, you won't be surprised to find Perl provides a way to subscript an array to refer to an element by a numeric index. The
3.4. List Assignment In much the same way as scalar values, list values may be assigned to variables: ($fred, $barney, $dino) = ("flintstone", "rubble", undef); All three variables in the list o
3.3. List Literals An array (the way you represent a list value within your program) is a list of comma-separated values enclosed in parentheses. These values form the elements of the list: (1,