⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch9.htm

📁 美国Macmillan出版社编写的Perl教程《Perl CGI Web Pages for WINNT》
💻 HTM
📖 第 1 页 / 共 4 页
字号:
<H3><A NAME="ThePerlDebugger">

The Perl Debugger</A></H3>

<P>

Perl comes equipped with its own debugger, perldb.pl, which is

invoked by using the -d switch at the Perl command line when running

perl.exe:

<BLOCKQUOTE>

<PRE>

perl -d script_name.pl

</PRE>

</BLOCKQUOTE>

<P>

When the Perl debugger is running, it goes through the script

specified with script_name.pl line by line. After the debugger

processes a line of the script, it will prompt you for input.

You can proceed with the need line, use any Perl command, or use

any number of debugger commands. The standard debugger commands

appear in Table 9.1.These debugger commands control the way the

debugger works on your script.<BR>

<P>

<CENTER><B>Table 9.1 Perl Debugger Commands</B></CENTER>

<P>

<CENTER>

<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>

<TR VALIGN=TOP><TD WIDTH=123><B>#_</B></TD><TD WIDTH=146><B>Debug Command</B>

</TD><TD WIDTH=335><B>Function</B></TD></TR>

<TR VALIGN=TOP><TD WIDTH=123>Line Control</TD><TD WIDTH=146>s</TD><TD WIDTH=335>Steps to the next line in the script

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=123>&nbsp;</TD><TD WIDTH=146>n

</TD><TD WIDTH=335>Goes to the next statement, skipping subroutines

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=123>&nbsp;</TD><TD WIDTH=146>c line_number

</TD><TD WIDTH=335>Signifies a one-time breakpoint at &quot;line_number&quot; to which the debugger continues until it is reached

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=123>&nbsp;</TD><TD WIDTH=146>&lt;CR&gt;

</TD><TD WIDTH=335>Repeats the last occurrence of the &quot;s&quot; or &quot;n&quot; command

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=123>&nbsp;</TD><TD WIDTH=146>/regular_expression/

</TD><TD WIDTH=335>Finds the pattern specified, searching forward in the script

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=123>&nbsp;</TD><TD WIDTH=146>?regular_expression?

</TD><TD WIDTH=335>Finds the pattern specified, searching backward in the script

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=123>&nbsp;</TD><TD WIDTH=146>A

</TD><TD WIDTH=335>Removes all line actions</TD></TR>

<TR VALIGN=TOP><TD WIDTH=123>Listing</TD><TD WIDTH=146>h</TD><TD WIDTH=335>Lists the debug commands 

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=123>&nbsp;</TD><TD WIDTH=146>V package variables

</TD><TD WIDTH=335>List the specified variables for the specified package, defaulting to the current package

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=123>&nbsp;</TD><TD WIDTH=146>X variables

</TD><TD WIDTH=335>Similar to the V command, but applied to current package 

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=123>&nbsp;</TD><TD WIDTH=146>f filename

</TD><TD WIDTH=335>Makes a switch to the file name specified and begins listing it

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=123>&nbsp;</TD><TD WIDTH=146>-

</TD><TD WIDTH=335>Lists the previous window of script</TD></TR>

<TR VALIGN=TOP><TD WIDTH=123>&nbsp;</TD><TD WIDTH=146>w line

</TD><TD WIDTH=335>Lists the window around the line number specified

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=123>&nbsp;</TD><TD WIDTH=146>1 min-max

</TD><TD WIDTH=335>Lists the script lines in the range specified in min-max

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=123>&nbsp;</TD><TD WIDTH=146>1

</TD><TD WIDTH=335>Lists the next window of script</TD></TR>

<TR VALIGN=TOP><TD WIDTH=123>&nbsp;</TD><TD WIDTH=146>1 line

</TD><TD WIDTH=335>Lists the script line specified</TD></TR>

<TR VALIGN=TOP><TD WIDTH=123>&nbsp;</TD><TD WIDTH=146>1 subroutine_name

</TD><TD WIDTH=335>Lists the script comprising the subroutine specified

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=123>&nbsp;</TD><TD WIDTH=146>L

</TD><TD WIDTH=335>Lists all the script lines that have breakpoints or actions

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=123>&nbsp;</TD><TD WIDTH=146>S

</TD><TD WIDTH=335>Lists the names of all script subroutines, prefixing them to the package in which they are declared

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=123>Declarations</TD><TD WIDTH=146>b line_condition

</TD><TD WIDTH=335>Declares a breakpoint in the debug search</TD>

</TR>

<TR VALIGN=TOP><TD WIDTH=123>&nbsp;</TD><TD WIDTH=146>b subroutine_condition

</TD><TD WIDTH=335>Declares a breakpoint at the beginning of a subroutine

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=123>&nbsp;</TD><TD WIDTH=146>a line_command

</TD><TD WIDTH=335>Declares an action for the specified lines

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=123>&nbsp;</TD><TD WIDTH=146>&lt; command

</TD><TD WIDTH=335>Declares an action to occur before each debugger prompt

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=123>&nbsp;</TD><TD WIDTH=146>&gt; command

</TD><TD WIDTH=335>Declares an action to occur after each debugger prompt

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=123>Miscellaneous</TD><TD WIDTH=146>! number</TD>

<TD WIDTH=335>Repeats a debugging command</TD></TR>

<TR VALIGN=TOP><TD WIDTH=123>&nbsp;</TD><TD WIDTH=146>! -number

</TD><TD WIDTH=335>Repeats the debugging command that occurred the specified number of debug commands previously

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=123>&nbsp;</TD><TD WIDTH=146>H-number

</TD><TD WIDTH=335>Shows the last specified number of debug commands

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=123>&nbsp;</TD><TD WIDTH=146>q or ^D<BR>Quits</TD><TD WIDTH=335>Executes specified command like a Perl statement, no semi-colon necessary 

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=123>&nbsp;</TD><TD WIDTH=146>p

</TD><TD WIDTH=335>Prints</TD></TR>

<TR VALIGN=TOP><TD WIDTH=123>&nbsp;</TD><TD WIDTH=146>T

</TD><TD WIDTH=335>For tracing stacks, lines starting with &quot;$ =&quot; are called in a scalar context, those starting with &quot;@ =&quot; are called in an array context

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=123>&nbsp;</TD><TD WIDTH=146>r</TD><TD WIDTH=335>Returns you from the present subroutine and executes all statements until the end of that subroutine

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=123>&nbsp;</TD><TD WIDTH=146>t</TD><TD WIDTH=335>Turns the trace mode on or off

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=123>&nbsp;</TD><TD WIDTH=146>d line</TD><TD WIDTH=335>Deletes the specified breakpoint

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=123>&nbsp;</TD><TD WIDTH=146>D</TD><TD WIDTH=335>Deletes all previous breakpoints

</TD></TR>

</TABLE></CENTER>

<P>

<P>

<CENTER>

<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>

<TR VALIGN=TOP><TD WIDTH=576><B>NOTE</B></TD></TR>

<TR VALIGN=TOP><TD WIDTH=576>

<BLOCKQUOTE>

<I>Some of the programming terms used in Table 9.1, like package, need an explanation. A package is an area of code in a script that protects its privacy. Variables and subroutines in a package are local to it only, like a script within a script. A 

breakpoint is a place in the debugging process where the debugger stops, or breaks, from its debugging. A window is the actual space supplied by perl.exe to view script lines in the Perl window. An action is the event caused by a command.</I>

</BLOCKQUOTE>



</TD></TR>

</TABLE></CENTER>

<P>

<H3><A NAME="CommonPerlErrors">

Common Perl Errors</A></H3>

<P>

Each language has its own peculiarities, and this holds true with

its bugs, too. At times, Perl uses some strange punctuation, so

checking this carefully is very important. This and other typical

errors are outlined below.

<P>

<B><FONT SIZE=2>Command Syntax Errors:</FONT></B>

<UL>

<LI>Forgetting to run the -w switch to find typographical errors

in your script. The -w switch must be set <I>before</I> you run

the Perl debugger, and cannot be used once the debugger is running,

like other debugger commands.

<LI>Replacing the elsif operator with elif or else if.

<LI>Accidentally clearing the regular expression values in the

special scalar variables $1 to $9, $', and $&amp;. These special

variables are always local to a statement block and are reset

with each new block. To store regular expressions beyond the block

try this script:<BR>

<TT>  ($one,$two) = /(\w+)(\w+)/;<BR>

</TT>which evalutes the variables in an array context.

<LI>Forgetting that by using the operator local() you reset the

name of the variable specified in all the other subroutines in

which it has scope.

<LI>Applying any loop control statements within a do/while loop.

This loop uses curly braces, {}, like a block statement, but it

is not a block statement and will fail if used as such.

<LI>Forgetting to use the chop() operator to remove &quot;/n&quot;

on any print statements being used for HTML output.

</UL>

<P>

<B>Perl Punctuation Errors:</B>

<UL>

<LI>Inserting a comma in a print statement after a filehandle

you don't want, as shown here:<BR>

<TT>  print filehandle_name, &quot;Hey,&quot;,&quot;now!&quot;;

<BR>

</TT>This would be wrong. You want<BR>

<TT>  print filehandle_name &quot;Hey,&quot;, &quot;now!&quot;;

<BR>

</TT>which is correct.

<LI>Omitting a statement ending semicolon, &quot;;&quot;.

<LI>Omitting a curly brace, &quot;{}&quot;, thus leaving a statement

block open.

</UL>

<P>

<B>Variable Names Errors:</B>

<UL>

<LI>Confusing the string operator eq with the numeric operator

== and ne with !=.

<LI>Using @example[1] instead of $example[1]. The first instance

is an array slice that creates an array of one element from an

array of many values. The second is a scalar, which can hold only

a single value.

</UL>

<P>

<B>Strings and Numbers Errors:</B>

<UL>

<LI>Forgetting the $ symbol at the start of a scalar variable.

Also forgetting the @ symbol at the start of an array, the % symbol

for an associated array, the &amp; symbol for subroutines, and

the * symbol for referencing all variable uses of that name.

</UL>

<P>

These various errors are among the most common to appear in Perl

scripts. The most frequently encountered errors are explained

in detail below.

<H3><A NAME="CommandSyntaxErrors">

Command Syntax Errors</A></H3>

<P>

Perl uses many different operators, some of which can be used

in any loop or subroutine, some of which cannot. Pay careful attention

to those operators by understanding what Perl control structures

can use which commands.

<P>

As an example, Perl uses two different operators to form relationships;

&quot;=&quot; and &quot;==&quot;. The &quot;=&quot; symbol is

the assignment operator, which is used to assign a value to a

variable. The &quot;==&quot; symbol is the equality operator,

which is used in an if statement to check the equality between

two numbers. A simple typing mistake can make this error quite

common in your scripts. The tricky element to this error is that,

at most times, it does not cause the script to fail, only to turn

out unusable data.

<P>

To check for this type of error, take note of what is going wrong

with the script in question. If the script is always treating

an if statement as being true, or if a variable's value changes

unexpectedly after a comparison, then you probably have a switched

assignment and/or equality operator.

<H3><A NAME="PerlPunctuationErrors">

Perl Punctuation Errors</A></H3>

<P>

The sometimes strange punctuation in Perl often causes mischief

unintentionally. This is a list of Perl's punctuation and the

typical problems concerning their use.

<UL>

<LI>The semicolon &quot;;&quot; must appear at the end of every

command line of Perl code.

<LI>The braces &quot;{}&quot; must be used in pairs around each

use of if, while, and sub statements, unless it is a short control

structure, like print &quot;hello&quot; if $w=1;. If the opening

or closing brace is missing where they are necessary, an error

will occur.

<LI>The parentheses &quot;()&quot; are not mandatory with every

Perl operator or command, but must be used with an if statement.

<LI>The quotation marks &quot;&quot; must be used in pairs, otherwise

errors will occur.

</UL>

<H3><A NAME="VariableNamesErrors">

Variable Names Errors</A></H3>

<P>

Perl makes use of many types of symbols to designate the different

kinds of variables, &quot;$&quot; for scalars, &quot;@&quot; for

arrays, &quot;%&quot; for associative arrays, and so forth, so

checking that these signifiers are used correctly is very important.

Using brackets, &quot;[]&quot;, with arrays, and curly braces,

&quot;{}&quot;, with associative arrays is also a must, but sometimes

these are switched and cause problems.

<H3><A NAME="StringsandNumbersErrors">

Strings and Numbers Errors</A></H3>

<P>

Perl uses different operators for strings and numbers, always

taking its lead from these operators on how to handle the text;

as a string or as a number. Check the string and numeric operators

table in Appendix A to make sure the ones in your script are correct.

<H2><A NAME="DebuggingtheCGI"><FONT SIZE=5 COLOR=#FF0000>

Debugging the CGI</FONT></A></H2>

<P>

The problem with your script may not be one that causes the script

to fail to execute, but that when it executes it supplies the

wrong data to the CGI. These problems can be even more frustrating

because they involve several levels outside of the script, based

on client/server dynamics, such as having the wrong MIME header,

or HTML tags.

<H3><A NAME="CheckingHTMLOutput">

Checking HTML Output</A></H3>

<P>

The problem may not be with Perl at all, but with the HTML tags

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -