📄 ch9.htm
字号:
<HTML>
<HEAD>
<TITLE>Chapter 9 -- Portability Issues with Windows
and Perl </TITLE>
<META NAME="GENERATOR" CONTENT="Mozilla/3.0b5aGold (WinNT; I) [Netscape]">
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#CE2910">
<H1><FONT COLOR=#FF0000>Chapter 9</FONT></H1>
<H1><B><FONT SIZE=5 COLOR=#FF0000>Portability Issues with Windows
and Perl </FONT></B>
</H1>
<P>
<HR WIDTH="100%"></P>
<P>
<H3 ALIGN=CENTER><FONT COLOR="#000000"><FONT SIZE=+2>CONTENTS<A NAME="CONTENTS"></A>
</FONT></FONT></H3>
<UL>
<LI><A HREF="#PerlforWindows" >Perl for Windows</A>
<LI><A HREF="#GettingtheDistribution" >Getting the Distribution</A>
<LI><A HREF="#DifferencesinPerlonNTandUNIX" >Differences in Perl on NT and UNIX</A>
<LI><A HREF="#UnsupportedFunctions" >Unsupported Functions</A>
<LI><A HREF="#ExtensionstoPerl5forWindowsNT" >Extensions to Perl 5 for Windows NT</A>
<UL>
<LI><A HREF="#EventLogFunctions" >Event Log Functions</A>
<LI><A HREF="#NTRegistryRoutines" >NT Registry Routines </A>
</UL>
<LI><A HREF="#ProblemswithWindows95" >Problems with Windows 95</A>
<LI><A HREF="#Summary" >Summary</A>
</UL>
<HR>
<P>
This chapter covers some of the issues concerning working with
Perl in the Microsoft Windows NT and Windows 95 environments.
After reading this chapter, you should be able to see where Perl
can be used on Windows platforms.
<H2><A NAME="PerlforWindows"><FONT SIZE=5 COLOR=#FF0000>Perl for
Windows</FONT></A></H2>
<P>
The Win32 port of Perl refers to both the Windows NT and Windows
95 environments. Actually, Windows 95 is not really a 32-bit environment,
though Perl 5 can still be used on it. The real work for the porting
process began with Windows 3.1 and has concentrated on Windows
NT 3.51. The work now includes limited support for Windows 95.
Taken collectively, the ported software is referred to as the
Win32 port of Perl 5 even though Window 95 is not a true 32-bit
operating system.
<P>
In any event, the Win32 version of Perl has lagged behind the
UNIX versions of Perl. For example, at the time this chapter was
written, Perl was already in 5.002b, whereas the Win32 versions
were at the 5.001m level. This is likely to be the case in the
near future because Perl has been developed primarily in the UNIX
domain and other platforms have been supported only as an afterthought.
Given the differences in operating systems and Perl's reliance
on certain system features, there exist some inconsistencies in
the versions of Perl 5 that run on UNIX and Windows NT. For example,
the Perl 5 for the Win32 port for NT is very different from the
UNIX version of this port because the Win32 version has several
module extensions that are not available in UNIX.
<P>
The port of Perl 5 to the Windows platform has even been blessed
by Microsoft to allow users to bring existing Perl scripts over
to NT servers. There is one major point to remember about the
Win32 port: the source for the extension is copyrighted by Microsoft
Corporation. However, the source is distributed under the Artistic
License agreement that comes with and governs the distribution
of Perl.
<P>
For more information, you can subscribe to the <TT><FONT FACE="Courier">Perl-Win32</FONT></TT>,
<TT><FONT FACE="Courier">Perl-Win32-users</FONT></TT>, and <TT><FONT FACE="Courier">Perl-Win32_announce</FONT></TT>
mailing lists. The <TT><FONT FACE="Courier">Perl-Win32</FONT></TT>
mailing list is for specific topics on using Perl and for differences
in scripts running on UNIX and NT systems. For other topics, especially
for newcomers, try the <TT><FONT FACE="Courier">Perl-Win32-users</FONT></TT>
list. Join the announcement list if you want to keep abreast of
any new ports. To join the discussions, send a mail message to
<TT><FONT FACE="Courier">majordomo@mail.hip.com</FONT></TT> and
in the body of the message add the single line <TT><FONT FACE="Courier">subscribe
Perl-Win32</FONT></TT>, <TT><FONT FACE="Courier">subscribe Perl-Win32_announce</FONT></TT>,
or <TT><FONT FACE="Courier">subscribe Perl-Win32-users</FONT></TT>,
depending on which mailing list you want to subscribe to.
<P>
There is also a WWW home page for Win32 Perl. This site contains
the latest archived information for Win32 Perl, including a FAQ
list. The URL for the Web site is <TT><A HREF="http://www.perl.hip.com/" tppabs="http://www.perl.hip.com/">http://www.perl.hip.com/</A></TT>.
Finally, check the CPAN archives for the <TT><FONT FACE="Courier">WINNT</FONT></TT>
package for more information on interfacing with the NT system.
<H2><A NAME="GettingtheDistribution"><FONT SIZE=5 COLOR=#FF0000>Getting
the Distribution</FONT></A></H2>
<P>
The Perl for Win32 distribution can be retrieved from <TT><FONT FACE="Courier">ftp.perl.hip.com</FONT></TT>.
Check out the latest version in the <TT><FONT FACE="Courier">/ntperl</FONT></TT>
directory. Both zipped source and binary files are available in
the <TT><FONT FACE="Courier">/ntperl/perl5.001m/CurrentBuild</FONT></TT>
directory.
<P>
The file with the binary version of Perl is <TT><FONT FACE="Courier">107-i86.zip</FONT></TT>;
the source (should you want to look at it) is in <TT><FONT FACE="Courier">107-src.zip</FONT></TT>.
Version 107 was dated December 1995 and might be out of date by
the time you read this, so use the latest version you can find.
The release notes in the <TT><FONT FACE="Courier">rel107.txt</FONT></TT>
file highlight significant improvements with every release. Always
read the release notes to see what functionality is available
for the port when compared with those in UNIX versions of Perl.
For example, it's only recently that some Perl on Windows 95 has
been supported.
<P>
Once you have the zip file, use <TT><FONT FACE="Courier">unzip</FONT></TT>
to extract the files. Do not use <TT><FONT FACE="Courier">pkunzip</FONT></TT>
because the names of the files you extract are longer than what
<TT><FONT FACE="Courier">pkzip</FONT></TT> supports. If you cannot
find <TT><FONT FACE="Courier">unzip</FONT></TT>, try using <TT><FONT FACE="Courier">WinZip</FONT></TT>.
Do not forget to specify that you want to create subdirectory
trees as you extract.
<P>
Read the <TT><FONT FACE="Courier">INSTALL.BAT</FONT></TT> file
to get an idea of how it all installs. Just run <TT><FONT FACE="Courier">INSTALL.BAT</FONT></TT>
and answer yes twice. When you unzip the Perl archive, remember
to use the option to preserve the directory structure. If you
do not unzip the directory tree, the <TT><FONT FACE="Courier">install.bat</FONT></TT>
file will fail with an error message stating that it cannot find
Perl. Also, there is a source code file, <TT><FONT FACE="Courier">107-src.zip</FONT></TT>,
that you do not need unless you make modifications to Perl or
compile in extensions. The source files will compile under <TT><FONT FACE="Courier">gcc</FONT></TT>
for DOS or Microsoft's Visual C++ 2.5 or later.
<H2><A NAME="DifferencesinPerlonNTandUNIX"><FONT SIZE=5 COLOR=#FF0000>Differences
in Perl on NT and UNIX</FONT></A></H2>
<P>
There are many important issues you must be aware of when writing
Perl scripts that must run under NT and UNIX. First of all, there
is the issue of pathnames. In DOS, Windows NT, and Windows 95
you specify pathnames with a backslash operator, whereas in UNIX
you use the forward slash operator. The Win32 Perl port allows
either forward slashes or backslashes. A Perl script using forward
slashes in pathnames is more likely to be portable than a script
using backslashes. Besides that, Perl scripts will have to use
the backslash <I>twice</I> in strings to escape one instance of
a backslash from the Perl interpreter. Drive letters do not have
to change.
<P>
For example, the following pathname can be set using either of
the following:
<BLOCKQUOTE>
$myFile = "E:/users/default/datafile.txt";<BR>
<BR>
<TT><FONT FACE="Courier">$myFile = "E:\\users\\default\\datafile.txt";</FONT></TT>
</BLOCKQUOTE>
<P>
Second, Perl scripts often rely heavily on escaped shell commands.
For example, the command to copy a file in UNIX is <TT><FONT FACE="Courier">cp</FONT></TT>,
but under NT the command is <TT><FONT FACE="Courier">COPY</FONT></TT>.
A portable Perl script will have to either avoid relying on escaped
commands or use system calls instead. Even commands that have
the same name behave differently on UNIX and NT. For example,
the <TT><FONT FACE="Courier">date</FONT></TT> command in UNIX
returns the current date and time of day, whereas there are two
separate commands for <TT><FONT FACE="Courier">DATE</FONT></TT>
and <TT><FONT FACE="Courier">TIME</FONT></TT>, respectively, in
NT. Here's one way to set up commands for different operating
environments based on the value of the variable <TT><FONT FACE="Courier">$usingUNIX</FONT></TT>:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">if ($usingUNIX)<BR>
{<BR>
$directory = `ls`; #UNIX
version.<BR>
$copyCommand = `cp`; #UNIX
version.<BR>
}<BR>
else<BR>
{<BR>
$directory = `dir`; #Win32
version.<BR>
$copyCommand = `COPY`; #Win32
version.<BR>
}</FONT></TT>
</BLOCKQUOTE>
<P>
Command-line Perl scripts are not directly supported with the
Win32 Perl package. Perl scripts have to be in files on disk to
be executed. There is a utility called <TT><FONT FACE="Courier">pl2bat.exe</FONT></TT>
that wraps a batch file (<TT><FONT FACE="Courier">.bat</FONT></TT>)
around a Perl script. After you run <TT><FONT FACE="Courier">pl2bat</FONT></TT>
on your Perl script, you simply execute the newly created batch
file to execute your script.
<P>
Perl scripts written under UNIX terminate lines with a carriage
return (<TT><FONT FACE="Courier">CR</FONT></TT>, <TT><FONT FACE="Courier">0x0D</FONT></TT>).
Windows NT, however, terminates text lines with two characters:
a carriage return <I>and</I> a line feed (<TT><FONT FACE="Courier">CR</FONT></TT>-<TT><FONT FACE="Courier">LF</FONT></TT>
or <TT><FONT FACE="Courier">0x0D</FONT></TT> + <TT><FONT FACE="Courier">0x0A</FONT></TT>).
When porting Perl scripts over to your Windows NT machine from
UNIX or vice versa, you may have some work to do in order to keep
the <TT><FONT FACE="Courier">CR</FONT></TT>-<TT><FONT FACE="Courier">LF</FONT></TT>
and <TT><FONT FACE="Courier">CR</FONT></TT>-only outputs consistent.
<P>
Finally, the dynamic loader in Perl, <TT><FONT FACE="Courier">DynaLoader</FONT></TT>,
is not currently implemented in the NT version of Perl. The major
consequence of this is that in NT you cannot run any Perl extensions
that require dynamic loading.<P>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR VALIGN=TOP><TD><B>Tip</B></TD></TR>
<TR VALIGN=TOP><TD>
<BLOCKQUOTE>
The UNIX <TT><FONT FACE="Courier">#!/usr/bin/perl</FONT></TT> notation does not work with Perl scripts on NT.
</BLOCKQUOTE>
</TD></TR>
</TABLE></CENTER>
<P>
<P>
Listing 9.1 presents a sample Perl script that sends mail on an
NT machine. You can send mail messages on NT using Perl if you
communicate using one-way sockets. (Use the SMTP server on NT
to receive mail.) Any error messages from the SMTP server when
sending messages are for acknowledgments sent back to the sender.
These error messages can be ignored.
<HR>
<BLOCKQUOTE>
<B>Listing 9.1. Using Perl on Windows NT.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier"> 1 require "cgi-lib.pl";
<BR>
2<BR>
3 $yonder = 'baqra.ikra.com';<BR>
4 $port = 25; #
Use standard port<BR>
5 $AF_INET = 2;<BR>
6 $SOCK_STREAM = 1;<BR>
7 $sockaddr = 'S n a4 x9';<BR>
8<BR>
9 # -------------------------------------------------------------
<BR>
10 chop($hostname = `hostname`);<BR>
11<BR>
12 ($name,$aliases,$proto) = getprotobyname('tcp');<BR>
13 ($name,$aliases,$port) = getservbyname($port,'tcp') unless
$port =~/^\d+$/;;<BR>
14 ($name,$aliases,$type,$len,$myAddress) = gethostbyname($hostname);
<BR>
15 ($name,$aliases,$type,$len,$recvAddress) = gethostbyname($yonder);
<BR>
16<BR>
17 $mySelf = pack($sockaddr, $AF_INET, 0, $myAddress);<BR>
18 $receiver = pack($sockaddr, $AF_INET, $port, $recvAddress);
<BR>
19<BR>
20 # -------------------------------------------------------------
<BR>
21 # Make the socket filehandle<BR>
22 # -------------------------------------------------------------
<BR>
23<BR>
24 socket(MYSOCKET, $AF_INET, $SOCK_STREAM, $proto) || die "\n
Cannot open $!";<BR>
25 bind(MYSOCKET, $mySelf) || die "\n Cannot bind" ;
<BR>
26 connect(MYSOCKET, $receiver) || die "\n Cannot connect";
<BR>
27<BR>
28 #<BR>
29 # Any print S statements go to the socket<BR>
30 # Any other print statements go back to the Web browser<BR>
31 #<BR>
32 select MYSOCKET;<BR>
33<BR>
34 ReadParse (@in);<BR>
35 print "HELO $hostname\n";<BR>
36 print "MAIL FROM: <",$in{'From'},">\n";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -