perlfaq3.html

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

HTML
1,053
字号
<?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>perlfaq3 - Programming Tools</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>perlfaq3 - Programming Tools</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>
	<ul>

		<li><a href="#how_do_i_do__anything_">How do I do (anything)?</a></li>
		<li><a href="#how_can_i_use_perl_interactively">How can I use Perl interactively?</a></li>
		<li><a href="#is_there_a_perl_shell">Is there a Perl shell?</a></li>
		<li><a href="#how_do_i_find_which_modules_are_installed_on_my_system">How do I find which modules are installed on my system?</a></li>
		<li><a href="#how_do_i_debug_my_perl_programs">How do I debug my Perl programs?</a></li>
		<li><a href="#how_do_i_profile_my_perl_programs">How do I profile my Perl programs?</a></li>
		<li><a href="#how_do_i_crossreference_my_perl_programs">How do I cross-reference my Perl programs?</a></li>
		<li><a href="#is_there_a_prettyprinter__formatter__for_perl">Is there a pretty-printer (formatter) for Perl?</a></li>
		<li><a href="#is_there_a_ctags_for_perl">Is there a ctags for Perl?</a></li>
		<li><a href="#is_there_an_ide_or_windows_perl_editor">Is there an IDE or Windows Perl Editor?</a></li>
		<li><a href="#where_can_i_get_perl_macros_for_vi">Where can I get Perl macros for vi?</a></li>
		<li><a href="#where_can_i_get_perlmode_for_emacs">Where can I get perl-mode for emacs?</a></li>
		<li><a href="#how_can_i_use_curses_with_perl">How can I use curses with Perl?</a></li>
		<li><a href="#how_can_i_use_x_or_tk_with_perl">How can I use X or Tk with Perl?</a></li>
		<li><a href="#how_can_i_make_my_perl_program_run_faster">How can I make my Perl program run faster?</a></li>
		<li><a href="#how_can_i_make_my_perl_program_take_less_memory">How can I make my Perl program take less memory?</a></li>
		<li><a href="#is_it_safe_to_return_a_reference_to_local_or_lexical_data">Is it safe to return a reference to local or lexical data?</a></li>
		<li><a href="#how_can_i_free_an_array_or_hash_so_my_program_shrinks">How can I free an array or hash so my program shrinks?</a></li>
		<li><a href="#how_can_i_make_my_cgi_script_more_efficient">How can I make my CGI script more efficient?</a></li>
		<li><a href="#how_can_i_hide_the_source_for_my_perl_program">How can I hide the source for my Perl program?</a></li>
		<li><a href="#how_can_i_compile_my_perl_program_into_byte_code_or_c">How can I compile my Perl program into byte code or C?</a></li>
		<li><a href="#how_can_i_compile_perl_into_java">How can I compile Perl into Java?</a></li>
		<li><a href="#how_can_i_get___perl_to_work_on__msdos_nt_____">How can I get <code>#!perl</code> to work on [MS-DOS,NT,...]?</a></li>
		<li><a href="#can_i_write_useful_perl_programs_on_the_command_line">Can I write useful Perl programs on the command line?</a></li>
		<li><a href="#why_don_t_perl_oneliners_work_on_my_dos_mac_vms_system">Why don't Perl one-liners work on my DOS/Mac/VMS system?</a></li>
		<li><a href="#where_can_i_learn_about_cgi_or_web_programming_in_perl">Where can I learn about CGI or Web programming in Perl?</a></li>
		<li><a href="#where_can_i_learn_about_objectoriented_perl_programming">Where can I learn about object-oriented Perl programming?</a></li>
		<li><a href="#where_can_i_learn_about_linking_c_with_perl">Where can I learn about linking C with Perl?</a></li>
		<li><a href="#i_ve_read_perlembed__perlguts__etc___but_i_can_t_embed_perl_in_my_c_program__what_am_i_doing_wrong">I've read perlembed, perlguts, etc., but I can't embed perl in my C program; what am I doing wrong?</a></li>
		<li><a href="#when_i_tried_to_run_my_script__i_got_this_message__what_does_it_mean">When I tried to run my script, I got this message. What does it mean?</a></li>
		<li><a href="#what_s_makemaker">What's MakeMaker?</a></li>
	</ul>

	<li><a href="#author_and_copyright">AUTHOR AND COPYRIGHT</a></li>
</ul>
<!-- INDEX END -->

<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>perlfaq3 - Programming Tools ($Revision: 1.56 $, $Date: 2005/12/31 00:54:37 $)</p>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>This section of the FAQ answers questions related to programmer tools
and programming support.</p>
<p>
</p>
<h2><a name="how_do_i_do__anything_">How do I do (anything)?</a></h2>
<p>Have you looked at CPAN (see <a href="../../lib/Pod/perlfaq2.html">the perlfaq2 manpage</a>)?  The chances are that
someone has already written a module that can solve your problem.
Have you read the appropriate manpages?  Here's a brief index:</p>
<pre>
        Basics          <a href="../../lib/Pod/perldata.html">perldata</a>, <a href="../../lib/Pod/perlvar.html">perlvar</a>, <a href="../../lib/Pod/perlsyn.html">perlsyn</a>, <a href="../../lib/Pod/perlop.html">perlop</a>, <a href="../../lib/Pod/perlsub.html">perlsub</a>
        Execution       <a href="../../lib/Pod/perlrun.html">perlrun</a>, <a href="../../lib/Pod/perldebug.html">perldebug</a>
        Functions       <a href="../../lib/Pod/perlfunc.html">perlfunc</a>
        Objects         <a href="../../lib/Pod/perlref.html">perlref</a>, <a href="../../lib/Pod/perlmod.html">perlmod</a>, <a href="../../lib/Pod/perlobj.html">perlobj</a>, <a href="../../lib/Pod/perltie.html">perltie</a>
        Data Structures <a href="../../lib/Pod/perlref.html">perlref</a>, <a href="../../lib/Pod/perllol.html">perllol</a>, <a href="../../lib/Pod/perldsc.html">perldsc</a>
        Modules         <a href="../../lib/Pod/perlmod.html">perlmod</a>, <a href="../../lib/Pod/perlmodlib.html">perlmodlib</a>, <a href="../../lib/Pod/perlsub.html">perlsub</a>
        Regexes         <a href="../../lib/Pod/perlre.html">perlre</a>, <a href="../../lib/Pod/perlfunc.html">perlfunc</a>, <a href="../../lib/Pod/perlop.html">perlop</a>, <a href="../../lib/Pod/perllocale.html">perllocale</a>
        Moving to perl5 <a href="../../lib/Pod/perltrap.html">perltrap</a>, perl
        Linking w/C     <a href="../../lib/Pod/perlxstut.html">perlxstut</a>, <a href="../../lib/Pod/perlxs.html">perlxs</a>, <a href="../../lib/Pod/perlcall.html">perlcall</a>, <a href="../../lib/Pod/perlguts.html">perlguts</a>, <a href="../../lib/Pod/perlembed.html">perlembed</a>
        Various         <a href="http://www.cpan.org/misc/olddoc/FMTEYEWTK.tgz">http://www.cpan.org/misc/olddoc/FMTEYEWTK.tgz</a>
                        (not a man-page but still useful, a collection
                         of various essays on Perl techniques)</pre>
<p>A crude table of contents for the Perl manpage set is found in <a href="../../lib/Pod/perltoc.html">the perltoc manpage</a>.</p>
<p>
</p>
<h2><a name="how_can_i_use_perl_interactively">How can I use Perl interactively?</a></h2>
<p>The typical approach uses the Perl debugger, described in the
<code>perldebug(1)</code> manpage, on an &quot;empty&quot; program, like this:</p>
<pre>
    perl -de 42</pre>
<p>Now just type in any legal Perl code, and it will be immediately
evaluated.  You can also examine the symbol table, get stack
backtraces, check variable values, set breakpoints, and other
operations typically found in symbolic debuggers.</p>
<p>
</p>
<h2><a name="is_there_a_perl_shell">Is there a Perl shell?</a></h2>
<p>The psh (Perl sh) is currently at version 1.8. The Perl Shell is a shell
that combines the interactive nature of a Unix shell with the power of
Perl. The goal is a full featured shell that behaves as expected for
normal shell activity and uses Perl syntax and functionality for
control-flow statements and other things. You can get psh at
<a href="http://sourceforge.net/projects/psh/">http://sourceforge.net/projects/psh/</a> .</p>
<p>Zoidberg is a similar project and provides a shell written in perl,
configured in perl and operated in perl. It is intended as a login shell
and development environment. It can be found at <a href="http://zoidberg.sf.net/">http://zoidberg.sf.net/</a>
or your local CPAN mirror.</p>
<p>The Shell.pm module (distributed with Perl) makes Perl try commands
which aren't part of the Perl language as shell commands.  perlsh from
the source distribution is simplistic and uninteresting, but may still
be what you want.</p>
<p>
</p>
<h2><a name="how_do_i_find_which_modules_are_installed_on_my_system">How do I find which modules are installed on my system?</a></h2>
<p>You can use the ExtUtils::Installed module to show all installed
distributions, although it can take awhile to do its magic.  The
standard library which comes with Perl just shows up as &quot;Perl&quot; (although
you can get those with Module::CoreList).</p>
<pre>
        <span class="keyword">use</span> <span class="variable">ExtUtils::Installed</span><span class="operator">;</span>
</pre>
<pre>
        <span class="keyword">my</span> <span class="variable">$inst</span>    <span class="operator">=</span> <span class="variable">ExtUtils::Installed</span><span class="operator">-&gt;</span><span class="variable">new</span><span class="operator">();</span>
        <span class="keyword">my</span> <span class="variable">@modules</span> <span class="operator">=</span> <span class="variable">$inst</span><span class="operator">-&gt;</span><span class="variable">modules</span><span class="operator">();</span>
</pre>
<p>If you want a list of all of the Perl module filenames, you
can use File::Find::Rule.</p>
<pre>
        <span class="keyword">use</span> <span class="variable">File::Find::Rule</span><span class="operator">;</span>
</pre>
<pre>
        <span class="keyword">my</span> <span class="variable">@files</span> <span class="operator">=</span> <span class="variable">File::Find::Rule</span><span class="operator">-&gt;</span><span class="variable">file</span><span class="operator">()-&gt;</span><span class="variable">name</span><span class="operator">(</span> <span class="string">'*.pm'</span> <span class="operator">)-&gt;</span><span class="variable">in</span><span class="operator">(</span> <span class="variable">@INC</span> <span class="operator">);</span>
</pre>
<p>If you do not have that module, you can do the same thing
with File::Find which is part of the standard library.</p>
<pre>
    <span class="keyword">use</span> <span class="variable">File::Find</span><span class="operator">;</span>
    <span class="keyword">my</span> <span class="variable">@files</span><span class="operator">;</span>
</pre>
<pre>
    <span class="variable">find</span><span class="operator">(</span>
      <span class="keyword">sub</span><span class="variable"> </span><span class="operator">{</span>
        <span class="keyword">push</span> <span class="variable">@files</span><span class="operator">,</span> <span class="variable">$File::Find::name</span>
                <span class="keyword">if</span> <span class="keyword">-f</span> <span class="variable">$File::Find::name</span> <span class="operator">&amp;&amp;</span> <span class="regex">/\.pm$/</span>
        <span class="operator">},</span>
</pre>
<pre>
      <span class="variable">@INC</span>
      <span class="operator">);</span>
</pre>
<pre>
        <span class="keyword">print</span> <span class="keyword">join</span> <span class="string">"\n"</span><span class="operator">,</span> <span class="variable">@files</span><span class="operator">;</span>
</pre>
<p>If you simply need to quickly check to see if a module is
available, you can check for its documentation.  If you can
read the documentation the module is most likely installed.
If you cannot read the documentation, the module might not
have any (in rare cases).</p>
<pre>
        prompt% <a href="../../lib/Pod/perldoc.html">perldoc</a> Module::Name</pre>
<p>You can also try to include the module in a one-liner to see if
perl finds it.</p>
<pre>
        perl -MModule::Name -e1</pre>
<p>
</p>
<h2><a name="how_do_i_debug_my_perl_programs">How do I debug my Perl programs?</a></h2>
<p>Have you tried <code>use warnings</code> or used <a href="../../lib/Pod/perlrun.html#item__2dw"><code>-w</code></a>?  They enable warnings
to detect dubious practices.</p>
<p>Have you tried <code>use strict</code>?  It prevents you from using symbolic
references, makes you predeclare any subroutines that you call as bare
words, and (probably most importantly) forces you to predeclare your
variables with <a href="../../lib/Pod/perlfunc.html#item_my"><code>my</code></a>, <a href="../../lib/Pod/perlfunc.html#item_our"><code>our</code></a>, or <code>use vars</code>.</p>
<p>Did you check the return values of each and every system call?  The operating
system (and thus Perl) tells you whether they worked, and if not
why.</p>
<pre>
  <span class="keyword">open</span><span class="operator">(</span><span class="variable">FH</span><span class="operator">,</span> <span class="string">"&gt; /etc/cantwrite"</span><span class="operator">)</span>
    <span class="keyword">or</span> <span class="keyword">die</span> <span class="string">"Couldn't write to /etc/cantwrite: $!\n"</span><span class="operator">;</span>
</pre>
<p>Did you read <a href="../../lib/Pod/perltrap.html">the perltrap manpage</a>?  It's full of gotchas for old and new Perl
programmers and even has sections for those of you who are upgrading
from languages like <em>awk</em> and <em>C</em>.</p>
<p>Have you tried the Perl debugger, described in <a href="../../lib/Pod/perldebug.html">the perldebug manpage</a>?  You can
step through your program and see what it's doing and thus work out
why what it's doing isn't what it should be doing.</p>
<p>
</p>
<h2><a name="how_do_i_profile_my_perl_programs">How do I profile my Perl programs?</a></h2>
<p>You should get the Devel::DProf module from the standard distribution
(or separately on CPAN) and also use Benchmark.pm from the standard
distribution.  The Benchmark module lets you time specific portions of
your code, while Devel::DProf gives detailed breakdowns of where your
code spends its time.</p>
<p>Here's a sample use of Benchmark:</p>
<pre>
  <span class="keyword">use</span> <span class="variable">Benchmark</span><span class="operator">;</span>

⌨️ 快捷键说明

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