| ActivePerl User Guide |

# Windows NT/2000 and Windows 9x

- NAME
- DESCRIPTION
  - Where can I get Windows 2000 information?
  - Where can I get Windows NT information?
  - Where can I get Windows 9x information?
  - What's the equivalent of the shebang ("#!") syntax for Win32?
  - What's the equivalent of chmod for Win32?
  - How do I send email from ActivePerl?
  - How do I schedule jobs on Win32 platforms?
  - Where can I find Win32 ports of UNIX tools?
  - What is a Windows NT/2000 service?
  - How do I run a Perl script as a Windows NT service?
  - How do I set permissions on a file?
  - How do I associate Perl scripts with perl?
  - Why doesn't "perl -e" work?
  - What editors are available for Windows?
- AUTHOR AND COPYRIGHT

## NAME

ActivePerl-faq4 - Windows NT/2000 and Windows 9x

## DESCRIPTION

Useful information about Windows NT/2000 and Windows 9x for Perl developers

## Where can I get Windows 2000 information?

The help files enclosed with Windows 2000 are an excellent source of information and can answer most of your questions about Windows 2000.

World Wide Web support for Windows 2000 can be found at:

> http://support.microsoft.com/

## Where can I get Windows NT information?

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/

## Where can I get Windows 9x information?

Printed documentation comes with Windows 9x, as well as some documentation on the CD-ROM. Help files are available from the Start menu.

World Wide Web support for Windows 9x is at:

> http://www.microsoft.com/windows95/

> http://www.microsoft.com/windows98/

## 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 `perl myscript.pl`.

Although Win32 systems themselves don't use the shebang, the Apache webserver does. If you are using the Apache webserver on your Win32 system you should put the shebang line at the top of your CGI scripts.

Your shebang line should look something like:

     #!perl

For Windows NT 4.0/2000, 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/2000 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/2000 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/2000). 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 9x, nor does it work with Windows NT/2000 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 ActivePerl 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. The big advantage of this over file associations is that I/O redirection will work correctly.

pl2bat has a number of useful command line options to affect how the wrapping is done, what command line switches to pass to perl, etc. Running `perldoc pl2bat` at the command line will show a full description of these options.

PerlApp, available in the Perl Dev Kit creates standalone .exe files from Perl Scripts. Redirection works correctly on PerlApp .exe files.

## 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 `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?.

## How do I send email from ActivePerl?

You may come across scripts that send email with an external mail program, as in:

            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

Another alternative is Mail::Sender, which can be used like so:

            use Mail::Sender;

            $sender = new Mail::Sender {smtp => 'mail.yourdomain.com', from => 'your@address.com'};
            $sender->MailFile({to => 'some@address.com', subject => 'Here is the file',
                           msg => "I'm sending you the list you wanted.",
                               file => 'filename.txt'});

Or Mail::Sendmail, which can be used like this:

            use Mail::Sendmail;

            %mail = ( To      => 'you@there.com',
                      From    => 'me@here.com',
                      Message => "This is a minimalistic message"
                     );

            if (sendmail %mail) { print "Mail sent OK.\n" }
            else { print "Error sending mail: $Mail::Sendmail::error \n" }

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 ActivePerl on the World Wide Web?).

If you really need a sendmail equivalent for Windows, several such equivalents exist:

Blat

  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 98. The Blat home page is located at http://www.interlog.com/~tcharron/blat.html

sendmail

  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/

wrmail

  Another commercial mail product is wrmail, part of the slmail product from Seattle Labs. A free version is available at http://www.seattlelab.com/

## How do I schedule jobs on Win32 platforms?

The UNIX cron utility doesn't exist on Win32 platforms.

For Windows 2000, there is a scheduling application available. It can be accessed through the Start menu: `Start | Settings | Control Panel | Scheduled Tasks`. By clicking "Add Scheduled Task", you start the Scheduled Task Wizard which walks you through the process.

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 the AT command seems to be very picky about syntax. Here is one example:

            at 23:37 /interactive /every:M,T,W,Th,F,S,Su cmd /c "c:\perl\bin\perl.exe c:\test.pl"

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. The "Schedule" service must be running when your job is supposed to happen.

Note that you may have problems when running AT or WinAT if the scheduled program or script requires any special rights or permissions other than those held by "System" (ie, if it is working across computers or NT domains). This is because NT does not properly allow you to run preinstalled NT services, such as the "Schedule" service as a user with network priveleges. It may look like you can set the scheduler to login as a user with network priveledges, but the service just does not recognize that user's right's and permissions and the System userid has only local permissions.

An all-Perl cron-like solution exists at http://www.megadodo.demon.co.uk/perl/ which uses a familiar Unix-like crontab file. This script can be ran as an NT Service (see How do I set up a Perl script as an NT Service?). When you create your own NT service, NT lets you set the useid/password pair, and these services do recognize that userid's rights and permissions. Hence, the scheduled program or script will have the necessary rights.

There are a few commercial cron-like schedulers. NTcrond, is available from ifdef software: http://www.ifdef.com/ and AutoTask2000 is available from http://www.cypressnet.com/. These programs should recognize the rights and priveleges of the userid you set their service to run under to enable the scheduled program or script to have the necessary rights.

For Windows 9x, 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/. There is also an optional "Task Scheduler" component with IE4.01 and Windows 98.

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.

In some instances, Perl's internal `sleep` function might be an appropriate means of scheduling. For example:

            $seconds = 180;
            sub action;
            while(1) {
                    &action;
                    sleep($seconds);
            }

## 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 9x commands to see if there's a rough equivalent distributed with your Win32 platform. If not, try one of these URLs:

- Cygwin - Linux-like environment for Windows, contains many GNU tools, as well as a complete development environment. http://www.cygwin.com/

- 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. Also, there are several UNIX-to-Win32 commercial packages available, including the MKS Toolkit from Mortice Kerns Systems, Inc.: http://www.mks.com/.

You can also check into the Perl Power Tools, being developed under the UNIX Reconstruction Project. Here you can find UNIX tools that are being reimplemented into all Perl. You can see the latest at http://language.perl.com/ppt/

## What is a Windows NT/2000 service?

On Windows NT/2000, 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/2000 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.

## How do I run a Perl script as a Windows NT Service?

You can run your Perl scripts as Windows NT Services via a program called srvany.exe, which comes with the Windows NT Resource Kit. Once srvany.exe is installed, read the srvany.wri file which should be with it. This document will explain how to set up registry entries for your new service.

After you are set up, to run your script as a service do:

      x:>srvany perl script.pl

## How do I run a Perl script as a Windows 2000 Service?

You can run your Perl scripts as Windows 2000 Services by accessing the Microsoft Management Console. The Windows 2000 help files contain plenty of information on how to start the MMC and how you can use it for Perl scripts.

## 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 9x), 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 9x/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, ActivePerl 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 2000, you can create a new file type and associate it with the perl intepreter with it as follows:

1. Double-click on the My Computer icon.

2. From the Tools menu, choose Folder Options. This opens the Folder Options dialog box.

3. Choose the File Types tab. This gives you a list of file types currently registered (or already associated in Windows 2000).

4. Click the New button.

5. In the File Extension box, type "`pl`" and click OK.

6. Click the Advanced button. This opens the Edit File Type window.

7. Click the New button. This opens the New Action dialog box. Type "`Open`" in the Action window (as you would want the Perl files to be "opened" by the Perl interpreter). Then, either type the full path of the location of the Path interpreter in the Application window, or click the Browse button to select the executable file of the Perl interpreter.

8. Click OK to close the New Action window. You can click the Change Icon button, if you want to find an icon to match the file type. When you click the Change Icon button, you are given a selection of pre-set icons or you can browse to a different location. If you want to use a Perl icon for the .pl files, browse to the folder where the Perl interpreter is located, choose the Perl interpreter and click Open. Then click OK to close the Change Icon Window.

9. Click OK to close the Edit File Type window.

10. Click Close to close the Folder Options window.

On Windows 9x and Windows NT 4.0, you can create a new file type and associate the perl interpreter with it as follows:

1. Open the My Computer icon on the Desktop. The My Computer window should appear.

2. From the View menu in the My Computer window, choose Options. The Options dialog box appears.

3. In the Options dialog box, select the File Types tab.

4. Click the New Type button. The Add New File Type dialog box appears.

5. In the "Description of type'' box, type "Perl Script''.

6. In the "Associated extension'' box, type ".pl''.

7. Leave the Content Type (MIME) box blank.

8. Click the New button beneath the Actions list. The New Action dialog box will appear.

9. In the Action box, type "Open'' (it's important to use this name for the action!).

10. 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).

11. Click OK to close the New Action dialog box.

12. Click OK to close the Add New File Type dialog box.

13. 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.)

## Why doesn't 'perl -e' work?

It does - it's just that some command shells (for example COMMAND.COM, CMD.EXE and 4DOS) don't accept single quotes wrapped around command arguments.

The safest way to do perl one-liners is to wrap the parameters within "" (double-quotes) the use q() and qq() inside the parameters. q() and qq() with put whatever is inside them between single-quotes or double-quotes, respectively.
For example:

> `perl -e "use LWP::Simple; while(@c = head <>) { $c = join qq(\n\t), @c; print qq(Header info:\n\t$c\n); }"`

(this was all one line)

Run this from the command line and type in the URL to your favorite website, like http://www.perl.com/.

The perlwin32 and perlrun POD pages have more information on using the command line.

## What editors are available for Windows?

Windows comes with two editors that are great for editing Perl, NotePad and WordPad. But, if you need more features for ease of editing, here is a list of free and commercial editors:

- Komodo - http://www.ActiveState.com/Products/Komodo/
  - Komodo is a complete, powerful development environment for Perl and other languages. Komodo also includes the Rx Toolkit, which provides the unique ability to interactively step through a regular expression as it is being matched, providing support for one of the most difficult technologies used in scripting languages.
- Visual Perl - http://www.ActiveState.com/Products/Visual_Perl/
  - Visual Perl is ActiveState's Perl plug-in for Visual Studio.NET. Within Visual Studio.NET, Visual Perl provides a full-featured integrated development environment that includes all the features of a modern IDE such as rich syntax aware editor, method tips, online help, and debugger support.
- MED - http://www.utopia-planitia.de/indexus.html
  - MED is a powerful shareware text editor for programmers, currently available for the platforms Windows 9x/NT4.0/2000.
- GNU Emacs for Windows - http://www.cs.washington.edu/homes/voelker/ntemacs.html
  - "Emacs is the extensible, customizable, self-documenting real-time display editor."
- VIM - http://www.vim.org
  - The "Vi IMproved" editor -- Now with Perl support. VIM provides Perl syntax highlighting, and a Perl interface. The Perl interface provides the ability to run a Perl command or run a Perl command for each line in a range. For more information on Perl support, see the Perl and VIM help page.

## AUTHOR AND COPYRIGHT

This FAQ was originally assembled and maintained by Evangelo Prodromou. It has been revised and updated by Brian Jepson of O'Reilly and Associates, and David Grove, David Dmytryshyn of ActiveState, Kevin Meltzer, Eric Smith and David Sparks of ActiveState.

This FAQ is in the public domain. If you use it, however, please ensure that you give credit to the original authors.

ActivePerl FAQ - Windows NT and Windows 9x