⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch9.htm

📁 《Perl 5 Unreleased》
💻 HTM
📖 第 1 页 / 共 5 页
字号:


</TD><TD WIDTH=270><TT><FONT FACE="Courier">wait()</FONT></TT>

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=229>

<BLOCKQUOTE>

<TT><FONT FACE="Courier">pipe()</FONT></TT>

</BLOCKQUOTE>



</TD><TD WIDTH=270><TT><FONT FACE="Courier">waitpid()</FONT></TT>

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=229>

<BLOCKQUOTE>

<TT><FONT FACE="Courier">setpriority()</FONT></TT>

</BLOCKQUOTE>



</TD><TD WIDTH=270></TD></TR>

<TR VALIGN=TOP><TD WIDTH=229><B>Miscellaneous Routines</B></TD><TD WIDTH=270>

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=229>

<BLOCKQUOTE>

<TT><FONT FACE="Courier">alarm()</FONT></TT>

</BLOCKQUOTE>



</TD><TD WIDTH=270><TT><FONT FACE="Courier">dump()</FONT></TT>

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=229>

<BLOCKQUOTE>

<TT><FONT FACE="Courier">dbmclose()</FONT></TT>

</BLOCKQUOTE>



</TD><TD WIDTH=270><TT><FONT FACE="Courier">syscall()</FONT></TT>

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=229>

<BLOCKQUOTE>

<TT><FONT FACE="Courier">dbmopen()</FONT></TT>

</BLOCKQUOTE>



</TD><TD WIDTH=270></TD></TR>

</TABLE></CENTER>

<P>

<P>

Perl scripts that make or rely heavily on <TT><FONT FACE="Courier">system()</FONT></TT>

calls are generally not portable from UNIX to NT.

<P>

You might consider writing <TT><FONT FACE="Courier">stub</FONT></TT>

routines for <TT><FONT FACE="Courier">system()</FONT></TT> and

<TT><FONT FACE="Courier">alarm()</FONT></TT> and include these

lines in any Perl code you port from UNIX to NT. This prevents

annoying compiler errors while you debug other sections of the

code. Keep in mind, though, that stubbing out these functions

is only an interim solution because the Perl script you are porting

over might actually use the return values from system calls.

<P>

Having a <TT><FONT FACE="Courier">null</FONT></TT> return value

from a fake system <TT><FONT FACE="Courier">stub</FONT></TT> function

most likely will work only in the simplest programs.

<H2><A NAME="ExtensionstoPerl5forWindowsNT"><FONT SIZE=5 COLOR=#FF0000>Extensions

to Perl 5 for Windows NT</FONT></A></H2>

<P>

Extensions for NT include support for working with the NT Registry

as well as event logging and OLE. (These extensions are meant

for NT only and most probably will not work in Windows 95.) The

OLE extensions require a deeper knowledge of OLE programming concepts,

which are well beyond the scope of this text and are not discussed

here. Check the <TT><FONT FACE="Courier">www.hip.perl.com</FONT></TT>

site for a short Web page with information about using OLE in

Perl scripts on NT. Use the Registry and event logging extensions

whenever possible instead of attempting to write your own modules

to do the same thing.

<P>

Here's a sample script in NT. To run this script, you have to

type <TT><FONT FACE="Courier">&quot;perl scriptName&quot;</FONT></TT>.

Note how the <TT><FONT FACE="Courier">#!</FONT></TT> is absent

in this script:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">print &amp;NTLoginName  , &quot;\n&quot;

;<BR>

print &amp;NTDomainName , &quot;\n&quot; ;<BR>

print &amp;NTNodeName , &quot;\n&quot; ;<BR>

print &amp;NTFsType , &quot;\n&quot; ;</FONT></TT>

</BLOCKQUOTE>

<P>

The utility functions provided under Windows NT Perl 5 and used

in this script are defined as follows:<P>

<CENTER>

<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>

<TR VALIGN=TOP><TD WIDTH=138><TT><FONT FACE="Courier">NTLoginName</FONT></TT>

</TD><TD WIDTH=297>Returns the ID of the user who's logged on

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=138><TT><FONT FACE="Courier">NTDomainName</FONT></TT>

</TD><TD WIDTH=297>Returns the domain name</TD></TR>

<TR VALIGN=TOP><TD WIDTH=138><TT><FONT FACE="Courier">NTNodeName</FONT></TT>

</TD><TD WIDTH=297>Returns the node name</TD></TR>

<TR VALIGN=TOP><TD WIDTH=138><TT><FONT FACE="Courier">NTFsType</FONT></TT>

</TD><TD WIDTH=297>Returns the name of the file system type</TD>

</TR>

</TABLE></CENTER>

<H3><A NAME="EventLogFunctions">Event Log Functions</A></H3>

<P>

The Win32 API allows for querying the event log in NT. Using the

<TT><FONT FACE="Courier">EventLog</FONT></TT> extensions, your

Perl script can access these API functions. For details on the

API calls themselves, check the NT API reference manual. Here

is a summary of the available functions:

<UL>

<LI><TT><FONT FACE="Courier">NTOpenEventLog HANDLE, $server, $source</FONT></TT>

<LI>This function opens the log specified by <TT><FONT FACE="Courier">$source</FONT></TT>

on the machine with name<I> </I><TT><FONT FACE="Courier">$server</FONT></TT>.

The file handle of the opened log, <TT><FONT FACE="Courier">HANDLE</FONT></TT>,

is used in subsequent operations. Use <TT><FONT FACE="Courier">NULL</FONT></TT>

if the server is the local machine.

<LI><TT><FONT FACE="Courier">NTCloseEventLog HANDLE</FONT></TT>

<LI>This function closes the event log associated with the supplied

<TT><FONT FACE="Courier">HANDLE</FONT></TT>.

<LI><TT><FONT FACE="Courier">NTGetNumberofEventLogRecords HANDLE,

$number</FONT></TT>

<LI>sing the <TT><FONT FACE="Courier">HANDLE</FONT></TT> of a

log opened with <TT><FONT FACE="Courier">NTOpenEventLog</FONT></TT>,

this function returns the number of records in <TT><FONT FACE="Courier">$number</FONT></TT>.

</UL>

<P>

It's easy to read each entry in the event log with a call to the

<TT><FONT FACE="Courier">NTReadEventLog</FONT></TT> function:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">NTReadEventLog HANDLE,<BR>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$flags,

<BR>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$record,

<BR>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$EVTheader,

<BR>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$source,

<BR>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$computer,

<BR>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$SID,

<BR>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$data,

<BR>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$strings</FONT></TT>

</BLOCKQUOTE>

<P>

Given the <TT><FONT FACE="Courier">HANDLE</FONT></TT> of a log

opened the <TT><FONT FACE="Courier">NTOpenEventLog</FONT></TT>

function attempts to read an entry from the log according to the

directives supplied in <TT><FONT FACE="Courier">$flags</FONT></TT>

and <TT><FONT FACE="Courier">$record</FONT></TT>. The <TT><FONT FACE="Courier">$flags</FONT></TT>

is a bit flag that can be any combination of the following values:

<UL>

<LI><TT><FONT FACE="Courier">EVENTLOG_FORWARDS_READ</FONT></TT>

<LI><TT><FONT FACE="Courier">EVENTLOG_BACKWARDS_READ</FONT></TT>

<LI><TT><FONT FACE="Courier">EVENTLOG_SEQUENTIAL_READ</FONT></TT>

<LI><TT><FONT FACE="Courier">EVENTLOG_SEEK_READ</FONT></TT>

</UL>

<P>

The <TT><FONT FACE="Courier">$record</FONT></TT> number specifies

the index to seek to and is numbered from 1 and up.

<P>

After the call is returned, you can look at the contents of the

rest of the arguments. The Event Record header (<TT><FONT FACE="Courier">$EVTheader</FONT></TT>)

is a binary structure. Use the <TT><FONT FACE="Courier">unpack()</FONT></TT>

routine to get its contents. Here's an example:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">( $length, $reserved, $recordnumber,

$timegenerated, $timewritten, $eventid,<BR>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$eventtype,

$numstrings, $eventcategory, $reservedflags,<BR>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$closingrecordnumber,

$stringoffset, $usersidlength, $usersidoffset,<BR>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$datalength,

$dataoffset ) = unpack( 'l6s4l6', $header );</FONT></TT>

</BLOCKQUOTE>

<P>

The <TT><FONT FACE="Courier">$source</FONT></TT> variable is set

to the name of the application that generated the event record.

<TT><FONT FACE="Courier">$computer</FONT></TT> specifies the Unc

name of the machine that generated the event record.

<P>

The <TT><FONT FACE="Courier">$SID</FONT></TT> variable stores

a security identifier structure as defined in the Win32 API documentation.

An array of message strings is returned in <TT><FONT FACE="Courier">$strings</FONT></TT>

as one long array of strings, each terminated with the <TT><FONT FACE="Courier">\0</FONT></TT>

character. The array is terminated with an empty string. (That

is, look for two <TT><FONT FACE="Courier">\0</FONT></TT> characters

to terminate the array.) The <TT><FONT FACE="Courier">$data</FONT></TT>

vatiable is set to any binary information associated with the

returned event record.

<P>

To add events to the log, use the <TT><FONT FACE="Courier">NTWriteEventLog</FONT></TT>

function. Here's the syntax for the <TT><FONT FACE="Courier">NTWriteEventLog</FONT></TT>

function:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">NTWriteEventLog<BR>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$computer,

<BR>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$source,

<BR>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$eventType,

<BR>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$category,

<BR>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$eventID,

<BR>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RESERVED,

<BR>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$data,

<BR>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@Strings</FONT></TT>

</BLOCKQUOTE>

<P>

This function writes an event log entry to the given event log

<TT><FONT FACE="Courier">$source</FONT></TT> on the named <TT><FONT FACE="Courier">$computer</FONT></TT>.

The <TT><FONT FACE="Courier">$eventType</FONT></TT> variable can

have any one of these values:

<UL>

<LI><TT><FONT FACE="Courier">EVENTLOG_ERROR_TYPE</FONT></TT>

<LI><TT><FONT FACE="Courier">EVENTLOG_WARNING_TYPE</FONT></TT>

<LI><TT><FONT FACE="Courier">EVENTLOG_INFORMATION_TYPE</FONT></TT>

</UL>

<P>

<TT><FONT FACE="Courier">$category</FONT></TT> specifies the event

category and is entirely determined by the calling application,

as is <TT><FONT FACE="Courier">$eventID</FONT></TT>. The <TT><FONT FACE="Courier">RESERVED</FONT></TT>

value must be set to <TT><FONT FACE="Courier">NULL</FONT></TT>.

<TT><FONT FACE="Courier">$data</FONT></TT> contains any binary

data that is to be associated with the event log record. <TT><FONT FACE="Courier">@Strings</FONT></TT>

is an array of strings that has the same format for the event

record that you read back.

<H3><A NAME="NTRegistryRoutines">NT Registry Routines </A></H3>

<P>

In Windows NT, all initialization (<TT><FONT FACE="Courier">*.INI</FONT></TT>)

files are now replaced with the Windows Registry. The Registry

is simply a database of the all the symbols, definitions, and

settings that used to exist in the INI files.<P>

<CENTER>

<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>

<TR VALIGN=TOP><TD ><B>Caution</B></TD></TR>

<TR VALIGN=TOP><TD >

<BLOCKQUOTE>

Although Perl lets you mess around with the Registry, it's important to remember that a wrong entry can mess up your NT system (not to mention your entire day). While tinkering with the Registry, you can easily render an NT machine useless, requiring a 

complete reinstall! Be careful.</BLOCKQUOTE>



</TD></TR>

</TABLE></CENTER>

<P>

<P>

The Win32 Perl routines do not support Unicode characters with

the Win32 API. Therefore, any Unicode strings required by the

mapped functions will be converted to 8-bit ANSI character strings.

<P>

The following functions are mapped directly to the Win32 API functions.

All functions return <TT><FONT FACE="Courier">true</FONT></TT>

on success and <TT><FONT FACE="Courier">false</FONT></TT> on failure.

The action of these functions in an NT environment is beyond the

scope of this book. Please consult the Win32 API for more information

concerning what the called functions do:<P>

<CENTER>

<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>

<TR VALIGN=TOP><TD WIDTH=176>NTRegCloseKey</TD><TD WIDTH=168>NTRegOpenKey

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=176>NTRegConnectRegistry</TD><TD WIDTH=168>NTRegOpenKeyEx

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=176>NTRegCreateKey</TD><TD WIDTH=168>NTRegQueryInfoKey

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=176>NTRegCreateKeyEx</TD><TD WIDTH=168>NTRegQueryValue

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=176>NTRegDeleteKey</TD><TD WIDTH=168>NTRegQueryValueEx

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=176>NTRegDeleteValue</TD><TD WIDTH=168>NTRegReplaceKey

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=176>NTRegEnumKey</TD><TD WIDTH=168>NTRegRestoreKey

</TD></TR>

<TR VALIGN=TOP><TD WIDTH=176>NTRegEnumKeyEx</TD><TD WIDTH=168>NTRegSaveKey

⌨️ 快捷键说明

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