| Win32 FAQ |
NAMEperlwin32faq4 - Windows NT and Windows 95
DESCRIPTIONUseful information about Windows NT and Windows 95 for Perl developers
A good starting place is the printed documentation that came with Windows NT and on the Windows NT CD in the support\books directory. Windows Help files are also available. You can get some in-depth information on Windows NT from the Windows NT Resource Kit, available at bookstores and from Microsoft. Microsoft TechNet is also a valuable resource. World Wide Web support for Windows NT Server is at:
http://www.microsoft.com/ntserver/ Support for Windows NT Workstation is at:
http://www.microsoft.com/ntworkstation/
Printed documentation comes with Windows 95, as well as some documentation on the CD-ROM. Help files are available from the Start menu. The Windows 95 Resource Kit contains in-depth information on Windows 95. It's available at bookstores and software stores. World Wide Web support for Windows 95 is at:
http://www.microsoft.com/windows95/
What's the equivalent of the shebang ("#!") syntax for Win32?
Unfortunately, Win32 platforms don't provide the shebang syntax, or
anything like it. You can try one of the two following methods to run a
script from the command line. If all else fails, you can always just call
the perl interpreter directly, as in
For Windows
NT 4.0, the coolest method is to use associated file
types (see
How do I associate Perl scripts with perl?). If you've associated Perl scripts with the .pl extension, you can just type the name of your file at the command line and Windows
NT will launch
perl.exe for you. If you change the
SET PATHEXT=.pl;%PATHEXT%
you can just type the file name without an extension, and Windows
NT will find the first .pl file in your path with that name. You may want to set
Given this setup, you can have a Perl script called cat.pl that looks like this:
while ( <> ) {
print;
}
and you can invoke it on the command line like this:
cat myfile.txt However, you can't invoke it with I/O redirection, like this:
cat < myfile.txt
cat myfile.txt | more
although it looks like you should be able to (this is a limitation of Windows NT). Your script can be in your path or you can provide a path to the file. Note that the file association method does not work for Windows 95, nor does it work with Windows NT if you have command extensions disabled. You can, however, still start the Perl script from an Explorer window if the extension is associated with perl. Another option is to use the pl2bat utility distributed with Perl for Win32 to convert your Perl script into a batch file. What this does is tag some Win32 batch language to the front of your script so that the system calls the perl interpreter on the file. It's quite a clever piece of batch coding, actually. If you call the pl2bat utility on your Perl script helloworld.pl, like this:
C:\> pl2bat helloworld.pl it will produce a batch file, helloworld.bat. You can then invoke the script just like this:
C:\> helloworld
Hello, World!
You can pass command line parameters, as well. Your script can be in your
pl2bat invokes perl.exe with the -w switch. If you don't like having the warning messages come up, and you know they're not important, you have to go in to the batch file and take the -w out yourself. The pl2exe utility is similar to pl2bat, but it puts an executable header on your perl script, producing an EXE file that cannot be directly edited. However, redirection works correctly under scripts that have been run through pl2exe.
What's the equivalent of chmod for Win32?
There is no direct equivalent of the chmod tool on Win32 systems. For file
attributes, you can use the ATTRIB command line tool (type
For information on the
How do I send email from Perl for Win32?You may come across scripts that send email with an external mail program, as in:
#!/usr/bin/perl
open(MAIL, '| /usr/lib/sendmail user@there.com') or die "$!";
print MAIL <<EOF;
To: user@there.com
From: user@here.com
Hello, World!
EOF
These sort of scripts generally cause people to ask, ``is there a sendmail
equivalent on Windows?'' If you need to send email from a Perl script,
there is no need to use an external program like sendmail. The libnet
bundle includes
use Net::SMTP;
$smtp = Net::SMTP->new('here.com'); # connect to an SMTP server
$smtp->mail( 'user@here.com' ); # use the sender's address here
$smtp->to('user@there.com'); # recipient's address
$smtp->data(); # Start the mail
# Send the header.
#
$smtp->datasend("To: user@there.com\n");
$smtp->datasend("From: user@here.com\n");
$smtp->datasend("\n");
# Send the body.
#
$smtp->datasend("Hello, World!\n");
$smtp->dataend(); # Finish sending the mail
$smtp->quit; # Close the SMTP connection
A Perl script for sending mail without using an external program is also available on Robin Chatterjee's Perl for Win32 page (see Are there information sources available on Perl for Win32 on the World Wide Web?). If you really need a sendmail equivalent for Windows, several such equivalents exist:
How do I schedule jobs on Win32 platforms?The UNIX cron utility doesn't exist on Win32 platforms.
For Windows
NT, a scheduling tool called
If you don't like the command-line version of A commercial cron-like scheduler, NTcrond, is available from ifdef software: http://www.ifdef.com/ For Windows 95, there's a System Agent available with the Microsoft Plus! Pack. Also, there are several shareware scheduling utilities, notably LaunchPad and Metz Scheduler. These can be found on a good shareware search engine, such as http://www.shareware.com/ Because scheduled jobs on Windows NT run as a service (see What is a Windows NT service?), you need to take special steps to make sure that files and environment variables are available to your script.
Where can I find Win32 ports of UNIX tools?You might want to take a look at the help file for Windows NT and Windows 95 commands to see if there's a rough equivalent distributed with your Win32 platform. If not, try one of these URLs:
There are also several UNIX-like tools available in the Windows NT Resource Kit. Finally, there are several UNIX-to-Win32 commercial packages available, including the MKS Toolkit from Mortice Kerns Systems, Inc.: http://www.mks.com/ and Interix from Softway Systems: http://www.interix.com/ (the product formerly known as OpenNT).
What is a Windows NT service?On Windows NT, a service is a special kind of executable program that runs in the background. Services are used for programs that are constantly working, such as network protocols or database servers. Most WWW servers on Windows NT are implemented as services. A service is different from other programs in several ways:
The most important thing to remember is that you have to take special steps
to make resources available to services. In general, you need to make files
available to the Everyone group, and you have to have environment variables
(like
How do I set permissions on a file?Win32 platforms don't have the same mechanisms for setting permissions on files as UNIX does. For files on FAT partitions (which means all files in Windows 95), you don't have to set permissions explicitly on a file. All files are available to all users. For files on an NTFS partition on Windows NT, you can set the security permissions on a file using the Explorer and the properties sheet of the file. Right-click the file in Explorer, and choose Properties from the drop-down menu. Select the Security tab, and click Permissions to set the Permissions on the file. Click Help for more information.
A command-line program, Windows 95/98 machines that are administered using Novell can have accounts and permissions similiar to NT. However, these permissions must be set up by the Novell administrator.
How do I associate Perl scripts with perl?On Windows systems, association is the process of specifying which programs should be used for which kind of files. Files are grouped into file types, such as JPEG files or Perl scripts. The file type of a file is identified by its file name extension (all the letters after the last ``.'' in the file name). So, for example, we can say that there's a type of file called a text file, which has the file extension .txt, and which is handled by the Notepad program. Usually, Perl for Win32 programmers create a file type like Perl Script and associate the extension .pl with that type. We specify that the perl interpreter binary, perl.exe, is responsible for that file type. Several Web servers require that you associate your scripts with perl.exe before the script can be run. On Windows 95 and Windows NT 4.0, you can create a new file type and associate the perl interpreter with it as follows:
You can test your association by double-clicking on a perl script in the Explorer window. If perl.exe starts and executes the script, things are OK. On Windows NT 4.0, you can avoid all the hassle of the above and just type the following from the command line:
ASSOC .pl=PerlScript
FTYPE PerlScript=[full path to perl]\perl.exe %1 %*
For more information on these commands, type Note that for this to work you have to have command extensions enabled. (These are enabled by default; you'd know if you'd turned them off.)
AUTHOR AND COPYRIGHTThis FAQ was originally assembled and maintained by Evangelo Prodromou. evangelo@endcontsw.com. It has been revised and updated by Brian Jepson of O'Reilly and Associates, and David Grove and David Dmytryshyn of ActiveState. This FAQ is in the public domain. If you use it, however, please ensure that you give credit to the original authors.
|
| Win32 FAQ |