40

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:

Community
  • 1
  • 1
Michael Carman
  • 30,628
  • 10
  • 74
  • 122

5 Answers5

62

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

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.

Warning: In principle, this should only be necessary on Windows XP. In my experience it's also necessary in Windows 7. In Windows 10 this is actively harmful—programs execute but produce nothing on stdout/stderr. The registry key needs to be set to 0 instead of 1.

See also:

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}"
}};
Community
  • 1
  • 1
Michael Carman
  • 30,628
  • 10
  • 74
  • 122
  • out of curiosity, why do you provide 4 different techniques for the above steps when all 4 can be achieved through registry manipulation? – ivancho Jan 18 '11 at 20:44
  • 7
    Well, "there's more than one way to do it" after all :) – Dallaylaen Jan 18 '11 at 20:59
  • 3
    @ivancho: The primary reason is that this is a collection of data gathered from various places. A secondary reason is an aversion to using a sledgehammer (registry editing) when a finishing hammer will suffice. That said, I would find a comprehensive "run this *.reg file" answer useful, if not terribly enlightening. – Michael Carman Jan 18 '11 at 21:04
  • @Michael. Great tips. My lack of being a Windows person bites me sometimes and the tip about redirection will make my life easier pretty well forever. Are there any side effects to it? – Nic Gibson Jan 18 '11 at 21:28
  • @Nic Gibson: It's a bug fix for Windows XP so it shouldn't have any side effects. The linked Microsoft kb article has the details. – Michael Carman Jan 18 '11 at 22:17
  • *Thank you!* The InheritConsoleHandles fix was the crucial part I was missing. This enables putting Perl scripts in locations in the PATH and never having to wonder whether `perl -S` is needed. (Still it bugs me that that KB article says the "problem was fixed in XP SP 1" yet you *still* need to manually perform the registry change... Implying that this will need to be done on every PC you work with! I'll make myself a .REG file to do this, but grrrrr...) – j_random_hacker Apr 12 '11 at 20:24
  • For others who have tried this, did the last item, Drag and Drop, take effect right away after running that script? Or did you have to reboot your machine? I just tried it, and I see that key in my registry now but when I drag a text file onto my Perl script it does not highlight. If I directly click the Perl script though, it does run in a console so the Perl script itself is being properly recognized as executable. – SSilk Aug 07 '14 at 19:21
  • 1
    `setx PATHEXT=%PATHEXT%;.PL` caused `ERROR: Invalid syntax.`. Replacing the `=` with a space ` ` worked. I would edit this answer, but I'm not a windows guru. Maybe someone else can verify that this is necessary and that I didn't make some other mistake and then we can edit the answer. – ArtOfWarfare Mar 18 '15 at 14:41
  • @ArtOfWarfare: Good catch. Updated. – Michael Carman Mar 18 '15 at 19:02
  • As of 5.24, Strawberry adds a Perl directory, i.e. c:\Strawberry\Perl\bin – hsmyers Nov 25 '16 at 18:11
  • For me, parameters to programs are not getting passed. If i print `$#ARGV` in a script, it says `-1` (no args passed). The `%*` is supposed to handle this, but doesn't seem to work for me. not sure if anything changed in win10? – Kip Jan 19 '17 at 16:24
  • 2
    Somehow on Windows10 I wasn't getting parameters to my scripts. I got it working by doing following additional steps (follow at your own risk!): delete registry keys: `HKCR/.pl`, `HKCR/pl_auto_file`, `HKCR/PerlScript`, `HKCU/SOFTWARE/Microsoft/Windows/CurrentVersion/Explorer/FileExts/.pl`. Then open task manager and kill all explorer.exe tasks (your taskbar goes away--don't worry), then in task manager do File/Run New Task and enter explorer.exe, then repeat the command-line commands in answer, then kill/restart explorer.exe. – Kip Jan 19 '17 at 18:50
  • Concerning the drop handler: What means "drag a file over a file"? Drag a perl script over a data file, drag a data file over a perl script, drag a perl script over a perl script or something different? – Ariser May 22 '18 at 16:22
  • 1
    @Ariser: The drop handler enables you to drag a data file (any file really, but typically data) and drop it onto a Perl script. It launches the script with file as an argument. – Michael Carman May 23 '18 at 15:04
  • @Kip, I had a similar problem to yours. I had set 'assoc' and 'ftype', but also had set Notepad text editor via the GUI, and when I tried to execute a script, Windows invoked Notepad to edit the script instead of running my script. I finally found salvation [here](https://superuser.com/questions/361816/pass-command-line-arguments-to-windows-open-with), which advises deleting some reigstry keys that Windows used in preference to assoc and ftype. – Some Guy Apr 23 '20 at 10:17
7

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.

Dallaylaen
  • 5,268
  • 20
  • 34
2

After following the instructions in the accepted answer, a double click still led to .pl files opening with Notepad in Windows 10 — even when perl.exe was set as the default file handler.

After finding Jack Wu's comment at ActivePerl. .pl files no longer execute but open in Notepad instead I was able to run perl scripts on double-click as such:

  • Select and right-click a .pl file
  • Use the "Open With" submenu to "Choose another app"
  • Select "Always use this app to open .pl files" (do this now – you won't get the chance after you have selected a program)
  • Scroll to the bottom of the "Other options" to find "More apps", and select "Look for another app on this PC"
  • Navigate to C:/path/to/perl/bin/ and select Perl5.16.3.exe (or the equivalent, depending on which version of Perl you have installed: but not Perl.exe)

Then the Perl icon appears next to .pl files and a double-click leads to them opening in Perl every time, as desired.

Community
  • 1
  • 1
Martin Smith
  • 3,687
  • 1
  • 24
  • 51
1

I tried the assoc and ftype methods and they didn't work for me.

What worked was editing this registry key:

Computer\HKEY_CURRENT_USER\Software\Classes\Applications\perl.exe\shell\open\command

It was set to: "C:\Perl64\bin\perl.exe" "%1"

When it should be: "C:\Perl64\bin\perl.exe" "%1" %*

It is the same content as the ftype, but for arcane windows reasons, I had to set it there too.

Nyvis
  • 11
  • 2
1

Like some others, I had set 'assoc' and 'ftype', but also had set Notepad text editor via the GUI, and when I tried to execute a script via the command line, Windows invoked Notepad to edit the script instead of running my script.

Using the GUI to instead point the .pl file association to the script-running executable was not much of an improvement, since it would invoke the executable on my script, but would pass no command-line arguments (even when I invoked my script from the command line).

I finally found salvation here which advised me to delete some registry keys.

Key quote: "The problem is that if you have already associated the program with the extension via the Open With dialog then you will have created an application association, instead of a file extension association, between the two. And application associations take precedence."

In my case, following the instructions to use RegEdit to delete

HKEY_CLASSES_ROOT \ Applications \ perl.exe

where perl.exe is the name of my Perl executable, and then also deleting:

HKEY_CLASSES_ROOT \ .pl

seemed to solve my problem, and then (after re-executing 'assoc' and 'ftype' commands as shown in other answers) I could then execute scripts from cmd.exe and have them run with access to their command-line parameters.

Some other related information here.

Some Guy
  • 405
  • 8
  • 15