Previous Table of Contents Next


Command-Line Flags

Command-line flags (also called switches) affect how Perl runs your program. If you read the introduction, you’ve seen one already: -v, which displays Perl’s 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.

You’ll be seeing a number of command-line flags throughout the book, and there’s a full list in Appendix G. But there’s one flag that you’ll 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 you’ll 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 aren’t any errors in hello, the behavior won’t change.

        % hello
RESULT: Hello world!

You can combine flags.

        % perl -we ‘ print “Two \n lines \n ”;’
RESULT: Two
        lines

Is Perl Compiled or Interpreted?

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 what’s going on?

Traditional compilers convert programs into machine language. When you run a Perl program, it’s first compiled into a bytecode, which is then converted (as the program runs) into machine instructions. So it’s 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. It’s somewhere in between, along with Python and awk and Emacs’ .elc files.

Quiz 1

What, a quiz already? Get used to it: There are 14 chapters, 8 sessions per chapter, 1 quiz per session. That’s 112 quizzes in all. Don’t take them too seriously—they’re meant to be instructional, not diagnostic. Some of the questions are very hard.

But relax—they’re multiple choice.

1.  Which of the following statements is false?
a.  Perl combines features of C with awk and UNIX shells.
b.  Perl 5 supports object-oriented programming.
c.  You compile Perl programs into machine language before running them.
d.  Perl is inexpensive.
2.  Which flag is used to execute Perl statements at the UNIX command line?
a.  -e
b.  -x
c.  -w
d.  You don’t need a flag.
3.  Nearly every Perl statement ends in
a.  #
b.  ;
c.  .
d.  !
4.  Perl comments begin with
a.  #
b.  ;
c.  //
d.  None of the above.

Exercise 1

Every session in this book has a programming exercise, and every chapter has four additional programming exercises at the end. That’s 168 exercises in all, which should keep you busy. They’re 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 exercise—the exercises don’t have unique “correct” solutions, and so no solutions are provided.

Difficulty: Easy

Write a program that prints your name.

Session 2
Scalars

Now let’s 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 you’ll 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”

Let’s run hello2, just to be sure.

        % hello2
RESULT: Hello world!


Previous Table of Contents Next