perldebtut.html

来自「perl教程」· HTML 代码 · 共 736 行 · 第 1/5 页

HTML
736
字号
<?xml version="1.0" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- saved from url=(0017)http://localhost/ -->
<script language="JavaScript" src="../../displayToc.js"></script>
<script language="JavaScript" src="../../tocParas.js"></script>
<script language="JavaScript" src="../../tocTab.js"></script>
<link rel="stylesheet" type="text/css" href="../../scineplex.css">
<title>perldebtut - Perl debugging tutorial</title>
<link rel="stylesheet" href="../../Active.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:" />
</head>

<body>

<script>writelinks('__top__',2);</script>
<h1><a>perldebtut - Perl debugging tutorial</a></h1>
<p><a name="__index__"></a></p>

<!-- INDEX BEGIN -->

<ul>

	<li><a href="#name">NAME</a></li>
	<li><a href="#description">DESCRIPTION</a></li>
	<li><a href="#use_strict">use strict</a></li>
	<li><a href="#looking_at_data_and_w_and_v">Looking at data and -w and v</a></li>
	<li><a href="#help">help</a></li>
	<li><a href="#stepping_through_code">Stepping through code</a></li>
	<li><a href="#placeholder_for_a__w__t__t">Placeholder for a, w, t, T</a></li>
	<li><a href="#regular_expressions">REGULAR EXPRESSIONS</a></li>
	<li><a href="#output_tips">OUTPUT TIPS</a></li>
	<li><a href="#cgi">CGI</a></li>
	<li><a href="#guis">GUIs</a></li>
	<li><a href="#summary">SUMMARY</a></li>
	<li><a href="#see_also">SEE ALSO</a></li>
	<li><a href="#author">AUTHOR</a></li>
	<li><a href="#contributors">CONTRIBUTORS</a></li>
</ul>
<!-- INDEX END -->

<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>perldebtut - Perl debugging tutorial</p>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>A (very) lightweight introduction in the use of the perl debugger, and a
pointer to existing, deeper sources of information on the subject of debugging
perl programs.</p>
<p>There's an extraordinary number of people out there who don't appear to know
anything about using the perl debugger, though they use the language every
day.  
This is for them.</p>
<p>
</p>
<hr />
<h1><a name="use_strict">use strict</a></h1>
<p>First of all, there's a few things you can do to make your life a lot more
straightforward when it comes to debugging perl programs, without using the
debugger at all.  To demonstrate, here's a simple script, named &quot;hello&quot;, with
a problem:</p>
<pre>
        <span class="comment">#!/usr/bin/perl</span>
</pre>
<pre>
        <span class="variable">$var1</span> <span class="operator">=</span> <span class="string">'Hello World'</span><span class="operator">;</span> <span class="comment"># always wanted to do that :-)</span>
        <span class="variable">$var2</span> <span class="operator">=</span> <span class="string">"$varl\n"</span><span class="operator">;</span>
</pre>
<pre>
        <span class="keyword">print</span> <span class="variable">$var2</span><span class="operator">;</span> 
        <span class="keyword">exit</span><span class="operator">;</span>
</pre>
<p>While this compiles and runs happily, it probably won't do what's expected,
namely it doesn't print &quot;Hello World\n&quot; at all;  It will on the other hand do
exactly what it was told to do, computers being a bit that way inclined.  That
is, it will print out a newline character, and you'll get what looks like a
blank line.  It looks like there's 2 variables when (because of the typo)
there's really 3:</p>
<pre>
        <span class="variable">$var1</span> <span class="operator">=</span> <span class="string">'Hello World'</span><span class="operator">;</span>
        <span class="variable">$varl</span> <span class="operator">=</span> <span class="keyword">undef</span><span class="operator">;</span>
        <span class="variable">$var2</span> <span class="operator">=</span> <span class="string">"\n"</span><span class="operator">;</span>
</pre>
<p>To catch this kind of problem, we can force each variable to be declared
before use by pulling in the strict module, by putting 'use strict;' after the
first line of the script.</p>
<p>Now when you run it, perl complains about the 3 undeclared variables and we
get four error messages because one variable is referenced twice:</p>
<pre>
 Global symbol &quot;$var1&quot; requires explicit package name at ./t1 line 4.
 Global symbol &quot;$var2&quot; requires explicit package name at ./t1 line 5.
 Global symbol &quot;$varl&quot; requires explicit package name at ./t1 line 5.
 Global symbol &quot;$var2&quot; requires explicit package name at ./t1 line 7.
 Execution of ./hello aborted due to compilation errors.</pre>
<p>Luvverly! and to fix this we declare all variables explicitly and now our
script looks like this:</p>
<pre>
        <span class="comment">#!/usr/bin/perl</span>
        <span class="keyword">use</span> <span class="variable">strict</span><span class="operator">;</span>
</pre>
<pre>
        <span class="keyword">my</span> <span class="variable">$var1</span> <span class="operator">=</span> <span class="string">'Hello World'</span><span class="operator">;</span>
        <span class="keyword">my</span> <span class="variable">$varl</span> <span class="operator">=</span> <span class="keyword">undef</span><span class="operator">;</span>
        <span class="keyword">my</span> <span class="variable">$var2</span> <span class="operator">=</span> <span class="string">"$varl\n"</span><span class="operator">;</span>
</pre>
<pre>
        <span class="keyword">print</span> <span class="variable">$var2</span><span class="operator">;</span> 
        <span class="keyword">exit</span><span class="operator">;</span>
</pre>
<p>We then do (always a good idea) a syntax check before we try to run it again:</p>
<pre>
        &gt; perl -c hello
        hello syntax OK</pre>
<p>And now when we run it, we get &quot;\n&quot; still, but at least we know why.  Just
getting this script to compile has exposed the '$varl' (with the letter 'l')
variable, and simply changing $varl to $var1 solves the problem.</p>
<p>
</p>
<hr />
<h1><a name="looking_at_data_and_w_and_v">Looking at data and -w and v</a></h1>
<p>Ok, but how about when you want to really see your data, what's in that
dynamic variable, just before using it?</p>
<pre>
        <span class="comment">#!/usr/bin/perl </span>
        <span class="keyword">use</span> <span class="variable">strict</span><span class="operator">;</span>
</pre>
<pre>
        <span class="keyword">my</span> <span class="variable">$key</span> <span class="operator">=</span> <span class="string">'welcome'</span><span class="operator">;</span>
        <span class="keyword">my</span> <span class="variable">%data</span> <span class="operator">=</span> <span class="operator">(</span>
                <span class="string">'this'</span> <span class="operator">=&gt;</span> <span class="string">qw(that)</span><span class="operator">,</span> 
                <span class="string">'tom'</span> <span class="operator">=&gt;</span> <span class="string">qw(and jerry)</span><span class="operator">,</span>
                <span class="string">'welcome'</span> <span class="operator">=&gt;</span> <span class="string">q(Hello World)</span><span class="operator">,</span>
                <span class="string">'zip'</span> <span class="operator">=&gt;</span> <span class="string">q(welcome)</span><span class="operator">,</span>
        <span class="operator">);</span>
        <span class="keyword">my</span> <span class="variable">@data</span> <span class="operator">=</span> <span class="keyword">keys</span> <span class="variable">%data</span><span class="operator">;</span>
</pre>
<pre>
        <span class="keyword">print</span> <span class="string">"$data{$key}\n"</span><span class="operator">;</span>
        <span class="keyword">exit</span><span class="operator">;</span>
</pre>
<p>Looks OK, after it's been through the syntax check (perl -c scriptname), we
run it and all we get is a blank line again!  Hmmmm.</p>

⌨️ 快捷键说明

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