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

📄 ch9.htm

📁 美国Macmillan出版社编写的Perl教程《Perl CGI Web Pages for WINNT》
💻 HTM
📖 第 1 页 / 共 4 页
字号:
records every error that occurs between client and service on

your server. Not all of these will be CGI errors, so look for

the listings that include the request method GET or POST; these

are the CGI errors. Watch out for the ever-increasing size of

error logs, such as the HTTP service error log. These can use

up a lot of memory very quickly if not kept under control, so

always keep a close watch on their size. Depending on how much

memory you have available, logs should not take up more than five

percent of your storage space. If you want to keep log data, you

can store it in compressed form, like a .zip archive.

<P>

For checking the error logs in NT you use Event Viewer to view

HTTP service logs. For example, the EMWAC HTTP service, or https,

logs its events in the Application Event Log, which can be viewed

using the Event Viewer. All the different errors that occur are

listed here, from I/O failures to system calls that run out of

resources. Any client errors are listed as Warning events.

<P>

There are four revealing pieces of information in the error messages.

These are the date and time of the error; the name of the client

that made the request that failed; the kind of error that occurred;

and the request method, GET or POST, that was used with the error.

Taken individually, these log listings are not really helpful.

But if you have a large number of them you can look for patterns

in the listings to see if certain client locations cause the errors,

a certain time of day might have something to do with it, and

so forth. All these are helpful clues in tracking down the source

of your problems.

<H3><A NAME="UsingthePrintOperator">

Using the Print Operator</A></H3>

<P>

This method may appear to be slow, but can actually help you find

the part of your script that is causing the rest to break down.

This technique is especially useful in larger scripts where variable

data changes are quite extensive. This method also allows you

to test the script in the CGI environment without having to access

the command line to do it.

<P>

When a script runs fine on your own computer, but fails when put

through the CGI, you insert &quot;print&quot; commands thoughout

the script in question. To illustrate we'll use a Perl script

with an error in it. (The error is indicated in parentheses.)

<BLOCKQUOTE>

<PRE>

if ($order{&quot;Payment&quot;} gt &quot; &quot;) {

          if ($order{&quot;Payment&quot;} ne $standard{&quot;Pay&quot;}) {

               $match = &Oslash;;

          } # this section matches type of payment

     } 

     if ($order{&quot;Product&quot;} gt &quot; &quot;) {

          if ($order{&quot;Product&quot;} ne $standard{&quot;Prod&quot;}) {

               $match = &Oslash;;

          } # this matches the product ordered

     }

     if ($order{&quot;Delivery&quot;} gt &quot; &quot;) {

          if ($order{&quot;Delivery&quot;} <I><B>(</B></I>!=<I><B>)</B></I> $standard{&quot;Del&quot;}) {

               $match = &Oslash;;

          } # this matches the delivery type

     }

</PRE>

</BLOCKQUOTE>

<P>

This example script is matching the method of payment, product

name/number, and delivery type of a customer order with specified

parcel carriers held in the database accessed by $standard, from

an associative array %standard. 

<P>

To check what is causing this error, you would first make a copy

of the script. Then, with this copy, insert print commands after

loop (remembering to add HTML tags, too) in the script, like this

<BLOCKQUOTE>

<PRE>

if ($order{&quot;Payment&quot;} gt &quot; &quot;) {

          if ($order{&quot;Payment&quot;} ne $standard{&quot;Pay&quot;}) {

               $match = &Oslash;;

          } # this section matches type of payment

     } 

     print &quot;&lt;HTML&gt;&lt;H2&gt;Payment Match is $match&lt;BR&gt;&quot;;

     if ($order{&quot;Product&quot;} gt &quot; &quot;) {

          if ($order{&quot;Product&quot;} ne $standard{&quot;Prod&quot;}) {

               $match = &Oslash;;

          } # this matches the product ordered

     }

     print &quot;Product Match is $match&lt;BR&gt;&quot;;

     if ($order{&quot;Delivery&quot;} gt &quot; &quot;) {

          if ($order{&quot;Delivery&quot;} <I><B>(</B></I>!=<I><B>)</B></I> $standard{&quot;Del&quot;}) {

               $match = &Oslash;;

          } # this matches the delivery type

     }

     print &quot;Delivery Match is $match&lt;BR&gt;&lt;/H2&gt;&lt;/HTML&gt;&quot;;

</PRE>

</BLOCKQUOTE>

<P>

The value of $match will be returned after each loop. The value

of &quot;1&quot; means a true value, or a match, and a value of

&quot;0&quot; means a false value, or no match. If you run the

above script you will get this output:

<BLOCKQUOTE>

<PRE>

Payment Match is 1

     Product Match is 1

     Delivery Match is &Oslash;

</PRE>

</BLOCKQUOTE>

<P>

so you know the script is failing in the third loop of this section.

The &quot;!=&quot; numeric operator was used in the third loop

instead of the correct &quot;ne&quot; string operator.

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

Perl Scripts for Debugging</FONT></A></H2>

<P>

To help you in your search for bugs, these Perl scripts will work

through your script and present HTML documents with their results.

The various tests they perform on your script are described with

the script listing.

<H3><A NAME="FindingtheEnvironment">

Finding the Environment</A></H3>

<P>

This script will print out the environmental variables used by

a script to an HTML document.

<BLOCKQUOTE>

<PRE>

#!/usr/bin/perl

# envtest.pl

     MAIN: {

          print &quot;Content-type: text/html\n\n&quot;;

          print &quot;&lt;HTML&gt;&lt;HEAD&gt;&lt;TITLE&gt;List of Environmental Variables&lt;/TITLE&gt;&lt;/HEAD&gt;&quot;;

          print &quot;&lt;BODY&gt;&lt;H2&gt;Environmental Variables Available to Your Script&lt;/H2&gt;&quot;;

          print &quot;&lt;H3&gt;&lt;UL&gt;&quot;;

          while (($key,$value) = each %ENV) {

               print &quot;&lt;LI&gt;$key = $value\n&quot;;

          }

          print &quot;&lt;/UL&gt;&lt;BR&gt;&quot;;

          print &quot;These are all the Environmental Variables available.&lt;/H3&gt;&lt;/BODY&gt;&lt;/HTML&gt;&quot;;

          exit &Oslash;;

     }

</PRE>

</BLOCKQUOTE>

<H3><A NAME="FindingtheGETValues">

Finding the GET Values</A></H3>

<P>

To find and display all the variables sent to a form request using

the GET method, use this script. The results are put into an HTML

document.

<BLOCKQUOTE>

<PRE>

#!/usr/bin/perl

     # gettest.pl

     MAIN: {

          print &quot;Content-type: text/html\n\n&quot;;

          print &quot;&lt;HTML&gt;&lt;HEAD&gt;&lt;TITLE&gt;List of GET Variables&lt;/TITLE&gt;&lt;/HEAD&gt;&quot;;

          print &quot;&lt;BODY&gt;&lt;H2&gt;Variables Sent Using GET&lt;/H2&gt;&quot;;

          print &quot;&lt;H3&gt;&lt;UL&gt;&quot;;

          $form_request = $ENV{'QUERY_STRING'};

          $pairs = split(/[&amp;=]/, $form_request));

     # splits name/value pairs

          foreach (%pairs) {

               tr/+/ /;

               s/%(..)/pack(&quot;c&quot;,hex($1))/ge;

          } # converts URL to ASCII

          while (($key,$value) = each %pairs) {

               print &quot;&lt;LI&gt;$key = $value\n&quot;;

          }

          print &quot;&lt;/UL&gt;&lt;BR&gt;&quot;;

          print &quot;These are all the GET variables sent.&lt;/H3&gt;&lt;/BODY&gt;&lt;/HTML&gt;&quot;;

          exit &Oslash;;

     }

</PRE>

</BLOCKQUOTE>

<H3><A NAME="FindingthePOSTValues">

Finding the POST Values</A></H3>

<P>

To find and display all the variables sent to a form request using

the POST method, use the following script. The results are put

into an HTML document.

<BLOCKQUOTE>

<PRE>

#! usr/bin/perl

     # posttest.pl

     MAIN: {

          print &quot;Content-type: text/html\n\n&quot;;

          print &quot;&lt;HTML&gt;&lt;HEAD&gt;&lt;TITLE&gt;List of POST Variables&lt;/TITLE&gt;&lt;/HEAD&gt;&quot;;

          print &quot;&lt;BODY&gt;&lt;H2&gt;Variables Sent Using POST&lt;/H2&gt;&quot;;

          print &quot;&lt;H3&gt;&lt;UL&gt;&quot;;

          read(STDIN, $form_request, $ENV{'CONTENT_LENGTH'}); # puts the POST data into 

     # STDIN and defines how many bytes to read

          $pairs = split(/[&amp;=]/, $form_request));

     # splits name/value pairs

          foreach (%pairs) {

               tr/+/ /;

               s/%(..)/pack(&quot;c&quot;,hex($1))/ge;

          } # converts URL to ASCII

          while (($key,$value) = each %pairs) {

               print &quot;&lt;LI&gt;$key = $value\n&quot;;

          }

          print &quot;&lt;/UL&gt;&lt;BR&gt;&quot;;

          print &quot;These are all the POST variables sent.&lt;/H3&gt;&lt;/BODY&gt;&lt;/HTML&gt;&quot;;

          exit &Oslash;;

     }

</PRE>

</BLOCKQUOTE>

<H3><A NAME="FindingtheDebuggingInformation">

Finding the Debugging Information</A></H3>

<P>

Similar to the debugging method of inserting print statements

into your script, this bug report subroutine, when inserted into

the script you are debugging, can be used to create a list of

the same print statements without requiring you to go to the trouble

of inserting all those print statements.

<P>

When you have finished debugging your script, you just set the

value of $debug to 0, effectively shutting it off.

<BLOCKQUOTE>

<PRE>

sub bugreport {

          if (debug == 1) {

               print &quot;Debug Report&quot;

               eval &quot;print @_&quot;; 

               print &quot;&lt;BR&gt;\n&quot;;

          }

     }

</PRE>

</BLOCKQUOTE>

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

Conclusion</FONT></A></H2>

<P>

Although there are many debugging techniques explored in this

chapter, the best offense is always a good defense. If you develop

good script writing habits to begin with, your troubles will be

fewer and far between. This development includes making use of

proper documentation in your scripts. If you are unsure whether

or not a note needs to be made, make it. It will take very little

time and space now, and may be invaluable in the future. If you

never have to use these notes, then you are luckier than most.

The programmers who come after you, who will have to work with

your script, will also be appreciative of your extra work.

<P>

It is also important to give yourself some time if you can't find

the bug. If you can, staying away from it for a couple of days

is best, but even a few hours rest will give you a better, fresher

perspective on the problem. To do this takes proper planning.

Always plan ahead and reserve some of your programming time to

debug. Just as writers have to incorporate editing time into their

deadlines, programmers have to set aside enough time to debug.

When making these plans, remember to involve any members of your

team, or department, who are needed for the project, or will need

to know your deadlines, as it may affect their deadlines. Don't

be afraid to ask for assistance. If another programmer has already

had a similar problem, then you can fix your problem that much

faster.

<P>

Although at times it may seem that you are expected to know everything,

you don't have to, because knowing where to get help is more valuable.

Especially if a problem persists, then have someone else read

through your script. This person should probably know Perl. Finally,

if a section is really causing you problems, a final step might

be just to delete it and write it over again. This time around

you might eliminate the error, or you might make one easier to

find.

<HR>



<CENTER><P><A HREF="ch8.htm" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/ch8.htm"><IMG SRC="PC.GIF" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/PC.GIF" BORDER=0 HEIGHT=88 WIDTH=140></A>

<A HREF="#CONTENTS"><IMG SRC="CC.GIF" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/CC.GIF" BORDER=0 HEIGHT=88 WIDTH=140></A>

<A HREF="contents.htm" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/contents.htm"><IMG SRC="HB.GIF" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/HB.GIF" BORDER=0 HEIGHT=88 WIDTH=140></A>

<A HREF="ch10.htm" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/ch10.htm"><IMG SRC="NC.GIF" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/NC.GIF" BORDER=0 HEIGHT=88 WIDTH=140></A>

<HR WIDTH="100%"></P></CENTER>

</BODY>

</HTML>

⌨️ 快捷键说明

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