[ Pobierz całość w formacie PDF ]
.rinet.ru:8080/CP7/ch13.htm (2 of 18) [2/17/2007 2:07:21 PM] Chapter 13 -- Debugging CGI Programsyour program contains the correct location for Perl.You might have to ask your Administrator for the correct location.Here'san example of a typical location:#!/usr/bin/perlYour program contains a syntax error.Perl checks syntax before executing the program and quits if it finds any errors.Checkthe program's syntax, as described in the next section.NoteYou'll look at the server's error log later in this chapter, in"Reading the Server Error Log." It can be an invaluable resourceif you happen to be the System Administrator or have the time tocontact her.Checking the Program's SyntaxThe first step in debugging a Perl program is to check its syntax.Perl is very picky about syntax errors and is very sensitive to them.A simple misspelling or a misplaced punctuation character can cause you hours of frustration if you aren't careful.In this section,you'll learn how to check your program's syntax and how to spot (and avoid) some of the most common syntax errors.NoteTechnically, a syntax error is an error in the language, formatting,or punctuation used to write the program.These errors often aretypographical errors.Checking Syntax at the Command LineWith Perl, it's quite easy to check your program's syntax.You should do this as part of the editing process.Personally, I check thesyntax each time I make a change.Type this command:perl -c programnameThis checks the syntax of the program without executing any of the commands.Alternatively, you simply can execute the programby typing its name.Perl checks the syntax before executing it and displays any errors it finds.You can use the -w switch to the Perl interpreter to give you additional information about debugging.This option watches forvariables that are used only once and other common errors.This command attempts to execute a Perl script calledregister.cgi, for example, and displays warnings:perl -w register.cgiInterpreting Perl Error MessagesA typical error message produced when you check syntax follows:syntax error at test.cgi line 29, near "while"syntax error at test.cgi line 129, near "}"test.cgi had compilation errors.Exit -1As you can see, Perl doesn't exactly spell out the exact cause and location of the error.However, it does give you two importantclues: The line number where the error occurred, and a bit of text near it.These are not exact; the line number often is incorrect, andthe quoted code often is unrelated to (but next to) the code with the problem.It's best to consider this a starting point for yourdebugging process.As this sample message illustrates, Perl often displays more than one error message.A good general rule is to ignore all but the firstmessage in the list.Why? Often, an error at one point in the program causes a later section to appear wrong, creating a second error.Fixing the first error often eliminates the second, so it's best to fix one error at a time and then to check the syntax again to seewhether you receive a different message.Looking At the Causes of Common Syntax ErrorsSome syntax errors are very easy to spot-for example, if you misspell the word print.Perl has some tricky syntax, however, andsome errors are much harder to detect.http://docs.rinet.ru:8080/CP7/ch13.htm (3 of 18) [2/17/2007 2:07:21 PM] Chapter 13 -- Debugging CGI ProgramsNow you'll look at some of the most common errors you can make when creating a Perl program and the error messages or othersymptoms they are likely to produce.NoteNot all syntax errors produce an error message.If a section ofyour program doesn't work or behaves in an unexpected manner,watch out for one of the errors described in this section.Punctuation ProblemsOne of the most basic syntax errors is incorrect punctuation.Because these errors can be created by a simple missed key on thekeyboard, they are quite common.Perl uses certain characters to indicate sections of the program or parts of a command.Table 13.1lists some errors to watch out for.Table 13.1.Common punctuation errors in Perl.Symbol Name Description; semicolon Each command in your Perl program must end witha semicolon.Unfortunately, the error message youget doesn't give you any hints.The error message inthe "Interpreting Perl Error Messages" section wascaused by this very error.The line listed in the errormessage is usually the line after the line missing thesemicolon.{ } braces Used to delimit sections of the program.The mostcommon problem is leaving off a closing brace tocorrespond with an opening brace.Fortunately, theerror message is right on target: Missing rightbracket.Remember that you need to use bracesafter each if, while, or sub statement.( ) parentheses Most of the commands in Perl do not requireparentheses.However, an if statement must useparentheses around the condition." " double Perl allows quoted strings to include multiple lines.quotation marks.This means that if you leave off a closingdouble quotation mark, the rest of your entireprogram might be considered part of the string.Assignment and Equality OperatorsOperators are used to form a relationship between two words in the program.The most common operator syntax error is also thehardest to notice.Remember that Perl uses two kinds of equal sign:The assignment operator (=) is used to assign a value to a variable.The equality operator (==) is used in an if statement's condition to test equality between two numbers.If you're like me, you'll run into this error constantly-usually, a simple typing mistake.What makes it so complicated is that theincorrect operator often does not cause a syntax error; instead, it just works differently than you are expecting.Consider thefollowing sample code:if ($result = 5) {print "The result is 5.";}This looks like a correct section of code-in fact, it would be perfectly acceptable insome languages.However, note that the assignment operator (=) has been used in the ifstatement when the equality operator (==) should have been used.What does this mean to the program? Well, instead of comparing the $result variable tothe constant 5, it is being assigned the value 5.Worse, Perl allows the assignment tobe used as a condition.The success of the assignment determines whether the conditionis true; in other words, instead of saying if the result is 5, you're saying if you canhttp://docs.rinet.ru:8080/CP7/ch13.htm (4 of 18) [2/17/2007 2:07:21 PM] Chapter 13 -- Debugging CGI Programssuccessfully make the result 5.Needless to say, this creates a problem.First of all, your condition always will beconsidered True, because the $result = 5 statement never fails.Second, and worse, your$result variable will be assigned the value 5, losing its previous value.Based on this scenario, you should remember the following clues, which might let youknow that you have mistakenly used the wrong type of equal sign:An if statement is treated as if it is always true.A variable changes value unexpectedly after a comparison.String and Numeric Equality OperatorsBefore you consider that if statement to be good, there's one more thing to check.Perl,unlike some languages, uses separate operators to refer to strings and numbers.Theequality operator, ==, is strictly for numbers.The operators are easy to remember, because the string operators usestrings -combinations of letters-instead of the normal punctuation.Table 13.2 gives asummary of the different operators for strings and numbers.Table 13.2.String and numeric operators in Perl.Condition Numeric Operator String OperatorIs equal to == eqDoes not equal != neIs greater than > gtGreater than or equal >= geIs less than test.pl file1 file2 file3 file4$0 equals test.pl.$#ARGV equals 3 [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • czarkowski.pev.pl
  •