perlwin32faq4 - Windows NT and Windows 95
Useful 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/
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 perl myscript.pl
.
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 PATHEXT
environment variable to include .pl files, like this:
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
PATHEXT
in the System control panel rather than on the command line. Otherwise,
you'll have to re-enter it each time the command prompt window closes.
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
PATH
, or in another directory, and the pl2bat code will usually find it and execute it correctly. As with file associations, however, you can't use
I/O redirection.
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.
There is no direct equivalent of the chmod tool on Win32 systems. For file
attributes, you can use the ATTRIB command line tool (type HELP
ATTRIB
at the command line for details). For more complex permissions, see How do I set permissions on a file?.
For information on the chmod()
function, see How does the chmod function work on Win32 platforms?.
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 Net::SMTP
, a module that can be used to send mail. Here is an example:
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:
Blat is a public domain program for sending email from the Windows NT command-line. Although it is reputed to work under Windows 95, the Blat authors have not tested it under Windows 95. The Blat home page is located at http://gepasi.dbs.aber.ac.uk/softw/Blat.html
Microsoft has a port of sendmail available on their FTP site at ftp://ftp.microsoft.com/developr/drg/unix-to-windows/ports/sendmail/
A commercial sendmail product is available from MetaInfo, Inc. An evaluation version is available at http://www.metainfo.com/
Another commercial mail product is wrmail, part of the slmail product from Seattle Labs. A free version is available at http://www.seattlelab.com/
The UNIX cron utility doesn't exist on Win32 platforms.
For Windows
NT, a scheduling tool called AT
is available. Unlike the
UNIX cron utility, AT
doesn't store its schedule in a flat file, but is configured using
command-line arguments. Note that the Schedule service must be running when
your job is supposed to happen.
If you don't like the command-line version of AT
, there's a
GUI version, WinAT, which is available with the Windows
NT Resource Kit.
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.
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:
GNU-Win32 - a Win32 port of many GNU tools, as well as a complete development environment. http://www.cygnus.com/misc/gnu-win32/
Virtually Un*x! - various ports of UNIX programs for Win32. http://www.itribe.net/virtunix
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).
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:
Services aren't run interactively, although the NET START
and NET
STOP
commands can be used to start or stop a server. The Services control panel
is used to start, stop, or pause a service.
Services don't act with the authority of the logged-in user. That means that services can't see user environment variables or read files that are readable only by the logged-in user. You can set the account that a service uses in the Services control panel.
Services run even when no one is logged in to the machine.
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 PATH
) be system environment variables.
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, CACLS
, will also change the permissions on a file. For more details, type HELP CACLS
on the command line.
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.
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:
Open the My Computer icon on the Desktop. The My Computer window should appear.
From the View menu in the My Computer window, choose Options. The Options dialog box appears.
In the Options dialog box, select the File Types tab.
Click the New Type button. The Add New File Type dialog box appears.
In the ``Description of type'' box, type ``Perl Script''.
In the ``Associated extension'' box, type ``.pl''.
Leave the Content Type (MIME) box blank.
Click the New button beneath the Actions list. The New Action dialog box will appear.
In the Action box, type ``Open'' (it's important to use this name for the action!).
In the ``Application used to perform action'' box, type [full path to
perl]\perl.exe %1 %*
, where [full path to perl] is the full path to
perl.exe on your machine. If perl is in your path, you can put just
perl.exe, but for esoteric reasons it's better to put the full path. Also, if the
path to your interpreter includes spaces (like C:\Program
Files\perl5) put in the
DOS path instead (C:\progra~1\perl5).
Click OK to close the New Action dialog box.
Click OK to close the Add New File Type dialog box.
Click OK to close the Options dialog box.
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 HELP FTYPE
at the command prompt.
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.)
This 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.