| Previous | Table of Contents | Next |
Command-line flags (also called switches) affect how Perl runs your program. If you read the introduction, youve seen one already: -v, which displays Perls version number.
% perl -v
RESULT: This is perl, version 5.001
Unofficial patchlevel 1m.
Copyright 1987-1994, Larry Wall
Perl may be copied only under the terms of either the Artistic
License or the GNU General Public License, which may be found in
the Perl 5.0 source kit.
Youll be seeing a number of command-line flags throughout the book, and theres a full list in Appendix G. But theres one flag that youll find especially useful: -e, which lets you execute Perl statements from the command line.
% perl -e print 4;
RESULT: 4
% perl -e print Hello world! \n;
RESULT: Hello world!
MS-DOS computers might not allow single quotes after the -e, in which case youll have to swap the single and double quotes:
% perl -e print Hello world!
RESULT: Hello world!
Just about anything you can do in a Perl program, you can do from the command line as well.
Command-line flags can also be placed inside programs, directly after #!/usr/bin/perl. Listing 1-2 shows hello with the -w flag, which warns the user about potential errors.
Listing 1-2 The hello program
#!/usr/bin/perl -w print Hello world! \n;
Because there arent any errors in hello, the behavior wont change.
% hello
RESULT: Hello world!
You can combine flags.
% perl -we print Two \n lines \n ;
RESULT: Two
lines
Good question. The short answer is interpreted, which means that your code can be run as is, without a compilation stage that creates a nonportable executable program. But this book occasionally mentions the Perl compiler. So whats going on?
Traditional compilers convert programs into machine language. When you run a Perl program, its first compiled into a bytecode, which is then converted (as the program runs) into machine instructions. So its not quite the same as shells, or Tcl, which are strictly interpreted without an intermediate representation. Nor is it like most versions of C or C++, which are compiled directly into a machine-dependent format. Its somewhere in between, along with Python and awk and Emacs .elc files.
What, a quiz already? Get used to it: There are 14 chapters, 8 sessions per chapter, 1 quiz per session. Thats 112 quizzes in all. Dont take them too seriouslytheyre meant to be instructional, not diagnostic. Some of the questions are very hard.
But relaxtheyre multiple choice.
Every session in this book has a programming exercise, and every chapter has four additional programming exercises at the end. Thats 168 exercises in all, which should keep you busy. Theyre graded in difficulty: Session exercises are rated Easy, Medium, or Hard; chapter exercises (there are always four) have an additional rating: Moderate. There are many radically different ways to solve each exercisethe exercises dont have unique correct solutions, and so no solutions are provided.
Difficulty: Easy
Write a program that prints your name.
Now lets make the example a bit more complex by introducing a variable: $greeting. The $ tells Perl (and you) that greeting is a scalar variable, such as the number 5.4 or the string Hello. (See Figure 1-1.) Other data types use different symbols; for example, arrays (which youll learn about later in the chapter) always begin with a @ and hashes (the subject of Chapter 5) always begin with a %.
Figure 1-1 Scalar variables
The hello2 program, shown in Listing 1-3, stores the string Hello in the scalar variable $greeting. When print() displays its string, it replaces $greeting with Hello, yielding the same output as the hello program.
Listing 1-3 hello2: Printing the value of $greeting, a scalar
#!/usr/bin/perl -w $greeting = Hello; # Set the value of $greeting to Hello print $greeting world!\n; # ...and print it, followed by world!\n
Lets run hello2, just to be sure.
% hello2
RESULT: Hello world!
| Previous | Table of Contents | Next |