perlwin32.html

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

HTML
1,020 行
字号
<dd>
<p>If you want perl to use globbing that emulates the quirks of DOS
filename conventions, you might want to consider using File::DosGlob
to override the internal <a href="../../lib/Pod/perlfunc.html#item_glob"><code>glob()</code></a> implementation.  See <a href="../../lib/File/DosGlob.html">the File::DosGlob manpage</a> for
details.</p>
</dd>
</li>
<dt><strong><a name="item_using_perl_from_the_command_line">Using perl from the command line</a></strong>

<dd>
<p>If you are accustomed to using perl from various command-line
shells found in UNIX environments, you will be less than pleased
with what Windows offers by way of a command shell.</p>
</dd>
<dd>
<p>The crucial thing to understand about the Windows environment is that
the command line you type in is processed twice before Perl sees it.
First, your command shell (usually CMD.EXE on Windows NT, and
COMMAND.COM on Windows 9x) preprocesses the command line, to handle
redirection, environment variable expansion, and location of the
executable to run. Then, the perl executable splits the remaining
command line into individual arguments, using the C runtime library
upon which Perl was built.</p>
</dd>
<dd>
<p>It is particularly important to note that neither the shell nor the C
runtime do any wildcard expansions of command-line arguments (so
wildcards need not be quoted).  Also, the quoting behaviours of the
shell and the C runtime are rudimentary at best (and may, if you are
using a non-standard shell, be inconsistent).  The only (useful) quote
character is the double quote (&quot;).  It can be used to protect spaces
and other special characters in arguments.</p>
</dd>
<dd>
<p>The Windows NT documentation has almost no description of how the
quoting rules are implemented, but here are some general observations
based on experiments: The C runtime breaks arguments at spaces and
passes them to programs in argc/argv.  Double quotes can be used to
prevent arguments with spaces in them from being split up.  You can
put a double quote in an argument by escaping it with a backslash and
enclosing the whole argument within double quotes.  The backslash and
the pair of double quotes surrounding the argument will be stripped by
the C runtime.</p>
</dd>
<dd>
<p>The file redirection characters &quot;&lt;&quot;, &quot;&gt;&quot;, and &quot;|&quot; can be quoted by
double quotes (although there are suggestions that this may not always
be true).  Single quotes are not treated as quotes by the shell or
the C runtime, they don't get stripped by the shell (just to make
this type of quoting completely useless).  The caret &quot;^&quot; has also
been observed to behave as a quoting character, but this appears
to be a shell feature, and the caret is not stripped from the command
line, so Perl still sees it (and the C runtime phase does not treat
the caret as a quote character).</p>
</dd>
<dd>
<p>Here are some examples of usage of the &quot;cmd&quot; shell:</p>
</dd>
<dd>
<p>This prints two doublequotes:</p>
</dd>
<dd>
<pre>
    perl -e &quot;print '\&quot;\&quot;' &quot;</pre>
</dd>
<dd>
<p>This does the same:</p>
</dd>
<dd>
<pre>
    perl -e &quot;print \&quot;\\\&quot;\\\&quot;\&quot; &quot;</pre>
</dd>
<dd>
<p>This prints &quot;bar&quot; and writes &quot;foo&quot; to the file &quot;blurch&quot;:</p>
</dd>
<dd>
<pre>
    <span class="variable">perl</span> <span class="keyword">-e</span> <span class="string">"print 'foo'; print STDERR 'bar'"</span> <span class="operator">&gt;</span> <span class="variable">blurch</span>
</pre>
</dd>
<dd>
<p>This prints &quot;foo&quot; (&quot;bar&quot; disappears into nowhereland):</p>
</dd>
<dd>
<pre>
    <span class="variable">perl</span> <span class="keyword">-e</span> <span class="string">"print 'foo'; print STDERR 'bar'"</span> <span class="number">2</span><span class="operator">&gt;</span> <span class="variable">nul</span>
</pre>
</dd>
<dd>
<p>This prints &quot;bar&quot; and writes &quot;foo&quot; into the file &quot;blurch&quot;:</p>
</dd>
<dd>
<pre>
    <span class="variable">perl</span> <span class="keyword">-e</span> <span class="string">"print 'foo'; print STDERR 'bar'"</span> <span class="number">1</span><span class="operator">&gt;</span> <span class="variable">blurch</span>
</pre>
</dd>
<dd>
<p>This pipes &quot;foo&quot; to the &quot;less&quot; pager and prints &quot;bar&quot; on the console:</p>
</dd>
<dd>
<pre>
    <span class="variable">perl</span> <span class="keyword">-e</span> <span class="string">"print 'foo'; print STDERR 'bar'"</span> <span class="operator">|</span> <span class="variable">less</span>
</pre>
</dd>
<dd>
<p>This pipes &quot;foo\nbar\n&quot; to the less pager:</p>
</dd>
<dd>
<pre>
    <span class="variable">perl</span> <span class="operator">-</span><span class="keyword">le</span> <span class="string">"print 'foo'; print STDERR 'bar'"</span> <span class="number">2</span><span class="operator">&gt;&amp;</span><span class="number">1</span> <span class="operator">|</span> <span class="variable">less</span>
</pre>
</dd>
<dd>
<p>This pipes &quot;foo&quot; to the pager and writes &quot;bar&quot; in the file &quot;blurch&quot;:</p>
</dd>
<dd>
<pre>
    <span class="variable">perl</span> <span class="keyword">-e</span> <span class="string">"print 'foo'; print STDERR 'bar'"</span> <span class="number">2</span><span class="operator">&gt;</span> <span class="variable">blurch</span> <span class="operator">|</span> <span class="variable">less</span>
</pre>
</dd>
<dd>
<p>Discovering the usefulness of the &quot;command.com&quot; shell on Windows 9x
is left as an exercise to the reader :)</p>
</dd>
<dd>
<p>One particularly pernicious problem with the 4NT command shell for
Windows NT is that it (nearly) always treats a % character as indicating
that environment variable expansion is needed.  Under this shell, it is
therefore important to always double any % characters which you want
Perl to see (for example, for hash variables), even when they are
quoted.</p>
</dd>
</li>
<dt><strong><a name="item_building_extensions">Building Extensions</a></strong>

<dd>
<p>The Comprehensive Perl Archive Network (CPAN) offers a wealth
of extensions, some of which require a C compiler to build.
Look in <a href="http://www.cpan.org/">http://www.cpan.org/</a> for more information on CPAN.</p>
</dd>
<dd>
<p>Note that not all of the extensions available from CPAN may work
in the Win32 environment; you should check the information at
<a href="http://testers.cpan.org/">http://testers.cpan.org/</a> before investing too much effort into
porting modules that don't readily build.</p>
</dd>
<dd>
<p>Most extensions (whether they require a C compiler or not) can
be built, tested and installed with the standard mantra:</p>
</dd>
<dd>
<pre>
    perl Makefile.PL
    $MAKE
    $MAKE test
    $MAKE install</pre>
</dd>
<dd>
<p>where $MAKE is whatever 'make' program you have configured perl to
use.  Use &quot;perl -V:make&quot; to find out what this is.  Some extensions
may not provide a testsuite (so &quot;$MAKE test&quot; may not do anything or
fail), but most serious ones do.</p>
</dd>
<dd>
<p>It is important that you use a supported 'make' program, and
ensure Config.pm knows about it.  If you don't have nmake, you can
either get dmake from the location mentioned earlier or get an
old version of nmake reportedly available from:</p>
</dd>
<dd>
<pre>
 <a href="http://download.microsoft.com/download/vc15/Patch/1.52/W95/EN-US/nmake15.exe">http://download.microsoft.com/download/vc15/Patch/1.52/W95/EN-US/nmake15.exe</a></pre>
</dd>
<dd>
<p>Another option is to use the make written in Perl, available from
CPAN.</p>
</dd>
<dd>
<pre>
    <a href="http://www.cpan.org/modules/by-module/Make/">http://www.cpan.org/modules/by-module/Make/</a></pre>
</dd>
<dd>
<p>You may also use dmake.  See <a href="#item_make">Make</a> above on how to get it.</p>
</dd>
<dd>
<p>Note that MakeMaker actually emits makefiles with different syntax
depending on what 'make' it thinks you are using.  Therefore, it is
important that one of the following values appears in Config.pm:</p>
</dd>
<dd>
<pre>
    make='nmake'        # MakeMaker emits nmake syntax
    make='dmake'        # MakeMaker emits dmake syntax
    any other value     # MakeMaker emits generic make syntax
                            (e.g GNU make, or Perl make)</pre>
</dd>
<dd>
<p>If the value doesn't match the 'make' program you want to use,
edit Config.pm to fix it.</p>
</dd>
<dd>
<p>If a module implements XSUBs, you will need one of the supported
C compilers.  You must make sure you have set up the environment for
the compiler for command-line compilation.</p>
</dd>
<dd>
<p>If a module does not build for some reason, look carefully for
why it failed, and report problems to the module author.  If
it looks like the extension building support is at fault, report
that with full details of how the build failed using the perlbug
utility.</p>
</dd>
</li>
<dt><strong><a name="item_command_2dline_wildcard_expansion">Command-line Wildcard Expansion</a></strong>

<dd>
<p>The default command shells on DOS descendant operating systems (such
as they are) usually do not expand wildcard arguments supplied to
programs.  They consider it the application's job to handle that.
This is commonly achieved by linking the application (in our case,
perl) with startup code that the C runtime libraries usually provide.
However, doing that results in incompatible perl versions (since the
behavior of the argv expansion code differs depending on the
compiler, and it is even buggy on some compilers).  Besides, it may
be a source of frustration if you use such a perl binary with an
alternate shell that *does* expand wildcards.</p>
</dd>
<dd>
<p>Instead, the following solution works rather well. The nice things
about it are 1) you can start using it right away; 2) it is more
powerful, because it will do the right thing with a pattern like
*/*/*.c; 3) you can decide whether you do/don't want to use it; and
4) you can extend the method to add any customizations (or even
entirely different kinds of wildcard expansion).</p>
</dd>
<dd>
<pre>
        <span class="variable">C</span><span class="operator">:\&gt;</span> <span class="variable">copy</span> <span class="variable">con</span> <span class="variable">c</span><span class="operator">:\</span><span class="variable">perl</span><span class="operator">\</span><span class="variable">lib</span><span class="operator">\</span><span class="variable">Wild</span><span class="operator">.</span><span class="variable">pm</span>
        <span class="comment"># Wild.pm - emulate shell @ARGV expansion on shells that don't</span>
        <span class="keyword">use</span> <span class="variable">File::DosGlob</span><span class="operator">;</span>
        <span class="variable">@ARGV</span> <span class="operator">=</span> <span class="keyword">map</span> <span class="operator">{</span>
                      <span class="keyword">my</span> <span class="variable">@g</span> <span class="operator">=</span> <span class="variable">File::DosGlob::glob</span><span class="operator">(</span><span class="variable">$_</span><span class="operator">)</span> <span class="keyword">if</span> <span class="regex">/[*?]/</span><span class="operator">;</span>
                      <span class="variable">@g</span> <span class="operator">?</span> <span class="variable">@g</span> <span class="operator">:</span> <span class="variable">$_</span><span class="operator">;</span>
                    <span class="operator">}</span> <span class="variable">@ARGV</span><span class="operator">;</span>
        <span class="number">1</span><span class="operator">;</span>
        <span class="operator">^</span><span class="variable">Z</span>
        <span class="variable">C</span><span class="operator">:\&gt;</span> <span class="variable">set</span> <span class="variable">PERL5OPT</span><span class="operator">=-</span><span class="variable">MWild</span>
        <span class="variable">C</span><span class="operator">:\&gt;</span> <span class="variable">perl</span> <span class="operator">-</span><span class="keyword">le</span> <span class="string">"for (@ARGV) { print }"</span> <span class="operator">*/*</span><span class="regex">/perl*.c
        p4view/perl</span><span class="operator">/</span><span class="variable">perl</span><span class="operator">.</span><span class="variable">c</span>
        <span class="variable">p4view</span><span class="operator">/</span><span class="variable">perl</span><span class="operator">/</span><span class="variable">perlio</span><span class="operator">.</span><span class="variable">c</span>
        <span class="variable">p4view</span><span class="operator">/</span><span class="variable">perl</span><span class="operator">/</span><span class="variable">perly</span><span class="operator">.</span><span class="variable">c</span>
        <span class="variable">perl5</span><span class="number">.005</span><span class="operator">/</span><span class="variable">win32</span><span class="operator">/</span><span class="variable">perlglob</span><span class="operator">.</span><span class="variable">c</span>
        <span class="variable">perl5</span><span class="number">.005</span><span class="operator">/</span><span class="variable">win32</span><span class="operator">/</span><span class="variable">perllib</span><span class="operator">.</span><span class="variable">c</span>
        <span class="variable">perl5</span><span class="number">.005</span><span class="operator">/</span><span class="variable">win32</span><span class="operator">/</span><span class="variable">perlglob</span><span class="operator">.</span><span class="variable">c</span>
        <span class="variable">perl5</span><span class="number">.005</span><span class="operator">/</span><span class="variable">win32</span><span class="operator">/</span><span class="variable">perllib</span><span class="operator">.</span><span class="variable">c</span>

⌨️ 快捷键说明

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