How do I make my Perl scripts act like normal programs on Windows?

I want my Perl scripts to behave just like any other executable (*.exe file).

  • When I double-click on myscript.pl I want it to execute instead of opening in a text editor.
  • I want to run myscript.pl instead of perl myscript.pl.
  • I really want to run myscript instead of myscript.pl.
  • I want to run program | myscript instead of program | perl myscript.pl.
  • I want to be able to run my script via drag & drop.

There are a number of changes you have to make on Windows to make all of these things work. Users typically stumble upon things that don’t work one at a time; leaving them confused whether they’ve made an error, there’s a bug in Perl, there’s a bug in Windows, or the behavior they want just isn’t possible. This question is intended to provide a single point of reference for making everything work up front; ideally before these problems even occur.

Related questions:

How do I make an executable to avoid command prompt on Windows? [duplicate]

This question already has an answer here: How do I make my Perl scripts act like normal programs on Windows? 4 answers The reason I’m asking this is because after half an hour of failing to fig

How do I profile my Perl programs?

I need to improve the performance of my Perl application. How can I find the slow spots? This is a question from the official perlfaq. We’re importing the perlfaq to Stack Overflow.

Building executable from perl scripts programs using PAR on Activestate Windows

I am used to build windows executable programs from perl scripts using Activestate PDK; now I would like to give a try to the PAR way to do the same thing – still using Activestate distribution: >

How do I make my toggle button act like a radio button?

I have 2 toggle buttons in my code and I want to change the behavior such that when one is selected the other one is not and vice versa. (want it to work like a radio button ).Any idea on how I can go

How to make Java look like Windows programs?

Is there a way (using Java) to make the GUIs that you create look like normal Windows programs? I don’t like the look and feel of the Java buttons and scrollers and stuff… It can use those if it’s r

How do I make Perl scripts recognize parameters in the Win32 cmd console?

When I invoke my Perl scripts in the Windows environment without invoking perl first, the parameters are not passed to my script. For example, C:/> C:/my-perl-scripts/foo.pl bar invokes foo.pl but

How can I make a batch file to act like a simple grep using Perl?

I already know the obvious answer to this question: just download <insert favorite windows grep or grep-like tool here>. However, I work in an environment with strict controls by the local IT

How do programs like mathematica draw graphs and how can I make such a program?

I’ve been wondering how programs like mathematica and mathlab, and so on, plot graphs of functions so gracefully and fast. Can anyone explain to me how they do this and, furthermore, how I can do this

How can I make a function act like a callback

How can I make shwOpts.show(fast); to act when removeHighlight() is done? I thought if I put an anonymous function as an argument in the other it will act as a callback. This didn’t work. removeHigh

Make my LinearLayout act like a button

I have a linear layout with custome views inside it and would like them to act like a button, so highlight itself when it is pressed. I looked into Drawable resources but cant quite understand how it

Answers

Note: The actions below require administrative privileges. For steps utilizing the command prompt it must be launched via “Run as administrator” on Windows Vista / Windows 7.

Associate *.pl files with perl

Run the following commands at a shell prompt:

assoc .pl=PerlScript
ftype PerlScript=C:/bin/perl.exe "%1" %*

Replace C:/Perl/bin/perl.exe with the path to your Perl installation. This enables you to run myscript.pl instead of perl myscript.pl.

Default install locations are:

  • ActivePerl: C:/Perl
  • Strawberry Perl: C:/Strawberry

Add .PL to your PATHEXT environment variable.

This makes Windows consider *.pl files to be executable when searching your PATH. It enables you to run myscript instead of myscript.pl.

You can set it for the current cmd session

set PATHEXT=%PATHEXT%;.PL

To set it permanently (under Windows Vista or Windows 7)

setx PATHEXT=%PATHEXT%;.PL

Under Windows XP you have to use the GUI:

  1. Right-click My Computer, and then click Properties.
  2. Click the Advanced tab.
  3. Click Environment variables.
  4. Select PATHEXT, then click Edit.
  5. Append ;.PL to the current value.

Make I/O redirection work (WinXP only)

I/O redirection (e.g. program | myscript) doesn’t work for programs started via a file association. There is a registry patch to correct the problem.

  1. Start Registry Editor.
  2. Locate and then click the following key in the registry: HKEY_LOCAL_MACHINE/Software/Microsoft/Windows/CurrentVersion/Policies/Explorer
  3. On the Edit menu, click Add Value, and then add the following registry value:
    • Value name: InheritConsoleHandles
    • Data type: REG_DWORD
    • Radix: Decimal
    • Value data: 1
  4. Quit Registry Editor.

See also STDIN/STDOUT Redirection May Not Work If Started from a File Association.

If patching the registry isn’t an option running program | perl -S myscript.pl is a less annoying work-around for scripts in your PATH.

Add a drop handler

Adding a drop handler for Perl allows you to run a Perl script via drag & drop; e.g. dragging a file over the file icon in Windows Explorer and dropping it there. Run the following script to add the necessary entries to the registry:

use Win32::TieRegistry;
$Registry->Delimiter("/");
$perlKey = $Registry-> {"HKEY_CLASSES_ROOT/Perl/"};
$perlKey-> {"shellex/"} = {
    "DropHandler/" =>  {
        "/" => "{86C86720-42A0-1069-A2E8-08002B30309D}"
}};

Convert your perl scripts into batch files using pl2bat once they are ready to be run by users.

The trick works through the perl -x switch which, according to perldoc perlrun, makes Perl search for the first line looking like #!.*perl.

I believe Michael Carman’s “Associate *.pl files with perl” has a small bug. The second command should read ftype PerlScript=C:/bin/perl.exe “%1” %*. Without the quotes around %1, any script that is in a path with spaces will fail.

The Win32 perl wiki entry has the correct syntax, but the Strawberry perl installer itself seems to use the version without the quotes. I’m not sure what the proper place to report the problem is.

Microsoft, in its infinite wisdom, decided to make the set command use an “=” but the setx command complains if you use the “=”! Thanks MS! 🙁