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 ofperl myscript.pl
. - I really want to run
myscript
instead ofmyscript.pl
. - I want to run
program | myscript
instead ofprogram | 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 f开发者_JAVA技巧ront; ideally before these problems even occur.
Related questions:
- How do I make Perl scripts recognize parameters in the Win32 cmd console?
- Running a perl script on windows without extension
- Perl execution from command line question
- How can I read piped input in Perl on Windows?
- Perl on Windows, file associations and I/O redirection
- How do I create drag-and-drop Strawberry Perl programs?
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:
- Right-click My Computer, and then click Properties.
- Click the Advanced tab.
- Click Environment variables.
- Select PATHEXT, then click Edit.
- 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.
- Start Registry Editor.
- Locate and then click the following key in the registry:
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer
- 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
- Value name:
- 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:
- STDIN/STDOUT Redirection May Not Work If Started from a File Association
- Perl Scripts on Windows 10 run from Explorer but not Command Prompt
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
.
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 notPerl.exe
)
Then the Perl icon appears next to .pl files and a double-click leads to them opening in Perl every time, as desired.
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.
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.
精彩评论