• Aucun résultat trouvé

Interpretive Languages Versus Compiled Languages

Dans le document Teach Yourself Perl 5 in 21 days (Page 46-49)

As you've seen, running a Perl program is easy. All you need to do is create the program, mark it as executable, and run it. The Perl interpreter takes care of the rest. Languages such as Perl that are processed by an interpreter are known as interpretive languages.

Some programming languages require more complicated processing. If a language is a compiled language, the program you write must be translated into machine-readable code by a special program known as a compiler. In addition, library code might need to be added by another special program known as a linker. After the compiler and linker have done their jobs, the result is a program that can be executed on your machine-assuming, of course, that you have written the

program correctly. If not, you have to compile and link the program all over again.

Interpretive languages and compiled languages both have advantages and disadvantages, as follows:

As you've seen with Perl, it takes very little time to run a program in an interpretive language.

Interpretive languages, however, cannot run unless the interpreter is available. Compiled programs, on the other hand, can be transferred to any machine that understands them.

As you'll see, Perl is as powerful as a compiled language. This means that you can do a lot of work quickly and easily.

Summary

Today you learned that Perl is a programming language that provides many of the capabilities of a high-level

programming language such as C. You also learned that Perl is easy to use; basically, you just write the program and run it.

You saw a very simple Perl program that reads a line of input from the standard input file and writes the line to the standard output file. The standard input file stores everything you type from your keyboard, and the standard output file stores everything your Perl program sends to your screen.

You learned that Perl programs contain a header comment, which indicates to the system that your program is written in Perl. Perl programs also can contain other comments, each of which must be preceded by a #.

Perl programs consist of a series of statements, which are executed one at a time. Each statement consists of a collection of tokens, which can be separated by white space.

Perl programs call library functions to perform certain predefined tasks. One example of a library function is print, which writes to the standard output file. Library functions are passed chunks of information called arguments; these arguments tell a function what to do.

The Perl interpreter executes the Perl programs you write. If it detects an error in your program, it displays an error message and uses the error-recovery process to try to continue processing your program. If Perl gets confused, error cascading can occur, and the Perl interpreter might display inappropriate error messages.

Finally, you learned about the differences between interpretive languages and compiled languages, and that Perl is an example of an interpretive language.

Q&A

Q: Is there any particular editor I need to use with Perl?

A: No. Perl programs are ordinary text files. You can use any text editor you like.

Q: Why do I need to enter the chmod +x command before running my program?

A: Because Perl programs are ordinary text files, the UNIX operating system does not know that they are executable programs. By default, text files have read and write permissions granted, which means you can look at your file or change it. The chmod +x command adds execute permission to the file; when this permission is granted, the system knows that this is an executable program.

Q: Can I use print to print other things besides input lines?

A: Yes. You'll learn more about how you can use print on Day 3, "Understanding Scalar Values."

Q: Why is Perl available for free?

A: This encourages the dissemination of computer knowledge and capabilities.

It works like this: You can get Perl for free, and you can use it to write interesting and useful programs. If you want, you can then give these programs away and let other people write interesting and useful programs based on your programs. This way, everybody benefits.

You also can modify the source for Perl, provided you tell everybody that your version is a modification of the original. This means that if you think of a clever thing you want Perl to do, you can add it yourself. (However, you can't blame anybody else if your modification breaks something or if it doesn't work.)

Of course, you don't have to give your Perl programs away for free. In fact, you even can sell your Perl programs, provided you don't borrow anything from somebody else's program.

Workshop

The Workshop provides quiz questions to help you solidify your understanding of the material covered and exercises to give you experience in using what you've learned. Try to understand the quiz and exercise answers before continuing to the next day.

Quiz

What do Perl's fans appreciate about Perl?

1.

What does the Perl interpreter do?

2.

Define the following terms:

a statement

What is a comment, and where can it appear?

4.

Where is Perl usually located on a UNIX machine?

5.

What is a header comment, and where does it appear in a program?

6.

What is a library function?

7.

Exercises

Modify program1_1 to print the input line twice.

1.

Modify program1_1 to read and print two different input lines.

2.

Modify program1_1 to read two input lines and print only the second one.

3.

BUG BUSTER: What is wrong with the following program?

#!/usr/local/bin/perl

$inputline = <STDIN>;

print ($inputline) 4.

BUG BUSTER: What is wrong with the following program?

#!/usr/local/bin/perl

$inputline = <STDIN>;

# print my line! print($inputline);

5.

What does the following program do?

#!/usr/local/bin/perl

$inputline = <STDIN>;

$inputline2 = <STDIN>;

print ($inputline2);

print ($inputline);

6.

Chapter 2

Dans le document Teach Yourself Perl 5 in 21 days (Page 46-49)