perlfaq3.html

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

HTML
1,053 行
字号
<p>In general, you can't do this.  There are some things that may work
for your situation though.  People usually ask this question
because they want to distribute their works without giving away
the source code, and most solutions trade disk space for convenience.
You probably won't see much of a speed increase either, since most
solutions simply bundle a Perl interpreter in the final product
(but see <a href="#how_can_i_make_my_perl_program_run_faster">How can I make my Perl program run faster?</a>).</p>
<p>The Perl Archive Toolkit ( <a href="http://par.perl.org/index.cgi">http://par.perl.org/index.cgi</a> ) is Perl's
analog to Java's JAR.  It's freely available and on CPAN (
<a href="http://search.cpan.org/dist/PAR/">http://search.cpan.org/dist/PAR/</a> ).</p>
<p>The B::* namespace, often called &quot;the Perl compiler&quot;, but is really a way
for Perl programs to peek at its innards rather than create pre-compiled
versions of your program.  However. the B::Bytecode module can turn your
script  into a bytecode format that could be loaded later by the
ByteLoader module and executed as a regular Perl script.</p>
<p>There are also some commercial products that may work for you, although
you have to buy a license for them.</p>
<p>The Perl Dev Kit ( <a href="http://www.activestate.com/Products/Perl_Dev_Kit/">http://www.activestate.com/Products/Perl_Dev_Kit/</a> )
from ActiveState can &quot;Turn your Perl programs into ready-to-run
executables for HP-UX, Linux, Solaris and Windows.&quot;</p>
<p>Perl2Exe ( <a href="http://www.indigostar.com/perl2exe.htm">http://www.indigostar.com/perl2exe.htm</a> ) is a command line
program for converting perl scripts to executable files.  It targets both
Windows and unix platforms.</p>
<p>
</p>
<h2><a name="how_can_i_compile_perl_into_java">How can I compile Perl into Java?</a></h2>
<p>You can also integrate Java and Perl with the
Perl Resource Kit from O'Reilly Media.  See
<a href="http://www.oreilly.com/catalog/prkunix/">http://www.oreilly.com/catalog/prkunix/</a> .</p>
<p>Perl 5.6 comes with Java Perl Lingo, or JPL.  JPL, still in
development, allows Perl code to be called from Java.  See jpl/README
in the Perl source tree.</p>
<p>
</p>
<h2><a name="how_can_i_get___perl_to_work_on__msdos_nt_____">How can I get <code>#!perl</code> to work on [MS-DOS,NT,...]?</a></h2>
<p>For OS/2 just use</p>
<pre>
    extproc perl -S -your_switches</pre>
<p>as the first line in <code>*.cmd</code> file (<a href="../../lib/Pod/perlrun.html#item__2ds"><code>-S</code></a> due to a bug in cmd.exe's
&quot;extproc&quot; handling).  For DOS one should first invent a corresponding
batch file and codify it in <code>ALTERNATE_SHEBANG</code> (see the
<em>dosish.h</em> file in the source distribution for more information).</p>
<p>The Win95/NT installation, when using the ActiveState port of Perl,
will modify the Registry to associate the <code>.pl</code> extension with the
perl interpreter.  If you install another port, perhaps even building
your own Win95/NT Perl from the standard sources by using a Windows port
of gcc (e.g., with cygwin or mingw32), then you'll have to modify
the Registry yourself.  In addition to associating <code>.pl</code> with the
interpreter, NT people can use: <code>SET PATHEXT=%PATHEXT%;.PL</code> to let them
run the program <code>install-linux.pl</code> merely by typing <code>install-linux</code>.</p>
<p>Under &quot;Classic&quot; MacOS, a perl program will have the appropriate Creator and
Type, so that double-clicking them will invoke the MacPerl application.
Under Mac OS X, clickable apps can be made from any <code>#!</code> script using Wil
Sanchez' DropScript utility: <a href="http://www.wsanchez.net/software/">http://www.wsanchez.net/software/</a> .</p>
<p><em>IMPORTANT!</em>: Whatever you do, PLEASE don't get frustrated, and just
throw the perl interpreter into your cgi-bin directory, in order to
get your programs working for a web server.  This is an EXTREMELY big
security risk.  Take the time to figure out how to do it correctly.</p>
<p>
</p>
<h2><a name="can_i_write_useful_perl_programs_on_the_command_line">Can I write useful Perl programs on the command line?</a></h2>
<p>Yes.  Read <a href="../../lib/Pod/perlrun.html">the perlrun manpage</a> for more information.  Some examples follow.
(These assume standard Unix shell quoting rules.)</p>
<pre>
    # sum first and last fields
    perl -lane 'print $F[0] + $F[-1]' *</pre>
<pre>
    <span class="comment"># identify text files</span>
    <span class="variable">perl</span> <span class="operator">-</span><span class="keyword">le</span> <span class="string">'for(@ARGV) {print if -f &amp;&amp; -T _}'</span> <span class="operator">*</span>
</pre>
<pre>
    <span class="comment"># remove (most) comments from C program</span>
    <span class="variable">perl</span> <span class="operator">-</span><span class="number">0777</span> <span class="operator">-</span><span class="variable">pe</span> <span class="string">'s{/\*.*?\*/}{}gs'</span> <span class="variable">foo</span><span class="operator">.</span><span class="variable">c</span>
</pre>
<pre>
    <span class="comment"># make file a month younger than today, defeating reaper daemons</span>
    <span class="variable">perl</span> <span class="keyword">-e</span> <span class="string">'$X=24*60*60; utime(time(),time() + 30 * $X,@ARGV)'</span> <span class="operator">*</span>
</pre>
<pre>
    <span class="comment"># find first unused uid</span>
    <span class="variable">perl</span> <span class="operator">-</span><span class="keyword">le</span> <span class="string">'$i++ while getpwuid($i); print $i'</span>
</pre>
<pre>
    <span class="comment"># display reasonable manpath</span>
    <span class="variable">echo</span> <span class="variable">$PATH</span> <span class="operator">|</span> <span class="variable">perl</span> <span class="operator">-</span><span class="variable">nl</span> <span class="operator">-</span><span class="number">072</span> <span class="keyword">-e</span> <span class="string">'
        s![^/+]*$!man!&amp;&amp;-d&amp;&amp;!$s{$_}++&amp;&amp;push@m,$_;END{print"@m"}'</span>
</pre>
<p>OK, the last one was actually an Obfuscated Perl Contest entry. :-)</p>
<p>
</p>
<h2><a name="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></h2>
<p>The problem is usually that the command interpreters on those systems
have rather different ideas about quoting than the Unix shells under
which the one-liners were created.  On some systems, you may have to
change single-quotes to double ones, which you must <em>NOT</em> do on Unix
or Plan9 systems.  You might also have to change a single % to a %%.</p>
<p>For example:</p>
<pre>
    # Unix
    perl -e 'print &quot;Hello world\n&quot;'</pre>
<pre>
    # DOS, etc.
    perl -e &quot;print \&quot;Hello world\n\&quot;&quot;</pre>
<pre>
    # Mac
    print &quot;Hello world\n&quot;
     (then Run &quot;Myscript&quot; or Shift-Command-R)</pre>
<pre>
    # MPW
    perl -e 'print &quot;Hello world\n&quot;'</pre>
<pre>
    # VMS
    perl -e &quot;print &quot;&quot;Hello world\n&quot;&quot;&quot;</pre>
<p>The problem is that none of these examples are reliable: they depend on the
command interpreter.  Under Unix, the first two often work. Under DOS,
it's entirely possible that neither works.  If 4DOS was the command shell,
you'd probably have better luck like this:</p>
<pre>
  perl -e &quot;print &lt;Ctrl-x&gt;&quot;Hello world\n&lt;Ctrl-x&gt;&quot;&quot;</pre>
<p>Under the Mac, it depends which environment you are using.  The MacPerl
shell, or MPW, is much like Unix shells in its support for several
quoting variants, except that it makes free use of the Mac's non-ASCII
characters as control characters.</p>
<p>Using qq(), q(), and qx(), instead of &quot;double quotes&quot;, 'single
quotes', and `backticks`, may make one-liners easier to write.</p>
<p>There is no general solution to all of this.  It is a mess.</p>
<p>[Some of this answer was contributed by Kenneth Albanowski.]</p>
<p>
</p>
<h2><a name="where_can_i_learn_about_cgi_or_web_programming_in_perl">Where can I learn about CGI or Web programming in Perl?</a></h2>
<p>For modules, get the CGI or LWP modules from CPAN.  For textbooks,
see the two especially dedicated to web stuff in the question on
books.  For problems and questions related to the web, like &quot;Why
do I get 500 Errors&quot; or &quot;Why doesn't it run from the browser right
when it runs fine on the command line&quot;, see the troubleshooting
guides and references in <a href="../../lib/Pod/perlfaq9.html">the perlfaq9 manpage</a> or in the CGI MetaFAQ:</p>
<pre>
        <a href="http://www.perl.org/CGI_MetaFAQ.html">http://www.perl.org/CGI_MetaFAQ.html</a></pre>
<p>
</p>
<h2><a name="where_can_i_learn_about_objectoriented_perl_programming">Where can I learn about object-oriented Perl programming?</a></h2>
<p>A good place to start is <a href="../../lib/Pod/perltoot.html">the perltoot manpage</a>, and you can use <a href="../../lib/Pod/perlobj.html">the perlobj manpage</a>,
<a href="../../lib/Pod/perlboot.html">the perlboot manpage</a>, <a href="../../lib/Pod/perltoot.html">the perltoot manpage</a>, <a href="../../lib/Pod/perltooc.html">the perltooc manpage</a>, and <a href="../../lib/Pod/perlbot.html">the perlbot manpage</a> for reference.</p>
<p>A good book on OO on Perl is the &quot;Object-Oriented Perl&quot;
by Damian Conway from Manning Publications, or &quot;Learning Perl
References, Objects, &amp; Modules&quot; by Randal Schwartz and Tom
Phoenix from O'Reilly Media.</p>
<p>
</p>
<h2><a name="where_can_i_learn_about_linking_c_with_perl">Where can I learn about linking C with Perl?</a></h2>
<p>If you want to call C from Perl, start with <a href="../../lib/Pod/perlxstut.html">the perlxstut manpage</a>,
moving on to <a href="../../lib/Pod/perlxs.html">the perlxs manpage</a>, <a href="../../lib/ExtUtils/xsubpp.html">the xsubpp manpage</a>, and <a href="../../lib/Pod/perlguts.html">the perlguts manpage</a>.  If you want to
call Perl from C, then read <a href="../../lib/Pod/perlembed.html">the perlembed manpage</a>, <a href="../../lib/Pod/perlcall.html">the perlcall manpage</a>, and
<a href="../../lib/Pod/perlguts.html">the perlguts manpage</a>.  Don't forget that you can learn a lot from looking at
how the authors of existing extension modules wrote their code and
solved their problems.</p>
<p>You might not need all the power of XS. The Inline::C module lets
you put C code directly in your Perl source. It handles all the
magic to make it work. You still have to learn at least some of
the perl API but you won't have to deal with the complexity of the
XS support files.</p>
<p>
</p>
<h2><a name="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></h2>
<p>Download the ExtUtils::Embed kit from CPAN and run `make test'.  If
the tests pass, read the pods again and again and again.  If they
fail, see <em>perlbug</em> and send a bug report with the output of
<code>make test TEST_VERBOSE=1</code> along with <code>perl -V</code>.</p>
<p>
</p>
<h2><a name="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></h2>
<p>A complete list of Perl's error messages and warnings with explanatory
text can be found in <a href="../../lib/Pod/perldiag.html">the perldiag manpage</a>. You can also use the splain program
(distributed with Perl) to explain the error messages:</p>
<pre>
    perl program 2&gt;diag.out
    splain [-v] [-p] diag.out</pre>
<p>or change your program to explain the messages for you:</p>
<pre>
    <span class="keyword">use</span> <span class="variable">diagnostics</span><span class="operator">;</span>
</pre>
<p>or</p>
<pre>
    <span class="keyword">use</span> <span class="variable">diagnostics</span> <span class="operator">-</span><span class="variable">verbose</span><span class="operator">;</span>
</pre>
<p>
</p>
<h2><a name="what_s_makemaker">What's MakeMaker?</a></h2>
<p>This module (part of the standard Perl distribution) is designed to
write a Makefile for an extension module from a Makefile.PL.  For more
information, see <a href="../../lib/ExtUtils/MakeMaker.html">the ExtUtils::MakeMaker manpage</a>.</p>
<p>
</p>
<hr />
<h1><a name="author_and_copyright">AUTHOR AND COPYRIGHT</a></h1>
<p>Copyright (c) 1997-2006 Tom Christiansen, Nathan Torkington, and
other authors as noted. All rights reserved.</p>
<p>This documentation is free; you can redistribute it and/or modify it
under the same terms as Perl itself.</p>
<p>Irrespective of its distribution, all code examples here are in the public
domain.  You are permitted and encouraged to use this code and any
derivatives thereof in your own programs for fun or for profit as you
see fit.  A simple comment in the code giving credit to the FAQ would
be courteous but is not required.</p>

</body>

</html>

⌨️ 快捷键说明

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