[ Pobierz całość w formacie PDF ]
.Error MessagesIf you incorrectly type a statement when creating a Perl program, the Perl interpreterdetects the error and tells you where the error is located.For example, look at Listing 29.3.This program is identical to the program you'vebeen seeing all along, except that it contains one small error.Can you spot it?Listing 29.3.Aprogram containing an error.1: #!/usr/local/bin/perl2: $inputline = <STDIN>3: print ($inputline);The output should give you a clue.$ program29_3Syntax error in file program29_3 at line 3, next char (Execution of program29_3 aborted due to compilation errors.$When you try to run this program, an error message appears.The Perl interpreterhas detected that line 2 of the program is missing its closing ; character.The error message from the interpreter tells you what the problem is and identifiesthe line on which the problem is located.TIP: You should fix errorsstarting from the beginning of your program and working down.When the Perl interpreterdetects an error, it tries to figure out what you meant to say and carries on fromthere; this feature is known as error recovery.Error recovery enables the interpreterto detect as many errors as possible at one time, which speeds up the developmentprocess.Sometimes, however, the Perl interpreter can get confused and think youmeant to do one thing when you really meant to do another.In this situation, theinterpreter might start trying to detect errors that don't really exist.This problemis known as error cascading.It's usually pretty easy to spot error cascading.Ifthe interpreter is telling you that errors exist on several consecutive lines, itusually means that the interpreter is confused.Fix the first error, and the othersmight very well go away.Interpretive LanguagesVersus Compiled LanguagesAs you've seen, running a Perl program is easy.All you need to do is create theprogram, mark it as executable, and run it.The Perl interpreter takes care of therest.Languages such as Perl that are processed by an interpreter are known as interpretivelanguages.Some programming languages require more complicated processing.If a languageis a compiled language, the program you write must be translated into machine-readablecode by a special program known as a compiler.In addition, library code might needto be added by another special program known as a linker.After the compiler andlinker have done their jobs, the result is a program that can be executed on yourmachine--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 mentioned here:As you've seen with Perl, it takes very little time to run a program in an interpretivelanguage.Interpretive languages, however, cannot run unless the interpreter is available.Compiled programs, on the other hand, can be transferred to any machine that understandsthem.As you'll see, Perl is as powerful as a compiled language.This means that youcan do a lot of work quickly and easily.SummaryIn this chapter you learned that Perl is a programming language that providesmany of the capabilities of a high-level programming language such as C.You alsolearned 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 standardinput file and writes the line to the standard output file.The standard input filestores everything you type from your keyboard, and the standard output file storeseverything your Perl program sends to your screen.You learned that Perl programs contain a header comment, which indicates to thesystem that your program is written in Perl.Perl programs also can contain othercomments, 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 whitespace.Perl programs call library functions to perform certain predefined tasks.Oneexample of a library function is print, which writes to the standard outputfile.Library functions are passed chunks of information called arguments; thesearguments tell a function what to do.The Perl interpreter executes the Perl programs you write.If it detects an errorin your program, it displays an error message and uses the error-recovery processto try to continue processing your program.If Perl gets confused, error cascadingcan occur, and the Perl interpreter might display inappropriate error messages.Finally, you learned about the differences between interpretive languages andcompiled languages, and that Perl is an example of an interpretive language
[ Pobierz całość w formacie PDF ]