如何让我的 Perl 脚本像 Windows 上的普通程序一样运行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4727480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I make my Perl scripts act like normal programs on Windows?
提问by Michael Carman
I want my Perl scripts to behave just like any other executable (*.exe file).
我希望我的 Perl 脚本的行为就像任何其他可执行文件(*.exe 文件)一样。
- 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.
- 当我双击时,
myscript.pl
我希望它执行而不是在文本编辑器中打开。 - 我想跑
myscript.pl
而不是perl myscript.pl
. - 我真的很想跑
myscript
而不是myscript.pl
. - 我想跑
program | myscript
而不是program | perl myscript.pl
. - 我希望能够通过拖放来运行我的脚本。
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'twork 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.
您必须在 Windows 上进行许多更改才能使所有这些功能正常工作。用户通常在东西绊倒不工作,一次一个; 让他们困惑是他们犯了错误,Perl 中存在错误,Windows 中存在错误,或者他们想要的行为是不可能的。这个问题旨在提供一个单一的参考点,让一切都在前面进行;理想情况下,甚至在这些问题发生之前。
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?
回答by Michael Carman
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.
注意:以下操作需要管理权限。对于使用命令提示符的步骤,它必须通过 Windows Vista / Windows 7 上的“以管理员身份运行”启动。
Associate *.pl files with perl
将 *.pl 文件与 perl 关联
Run the following commands at a shell prompt:
在 shell 提示符下运行以下命令:
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
.
替换C:\Perl\bin\perl.exe
为 Perl 安装路径。这使您能够运行myscript.pl
而不是perl myscript.pl
.
Default install locations are:
默认安装位置是:
- ActivePerl:
C:\Perl
- Strawberry Perl:
C:\Strawberry
- ActivePerl:
C:\Perl
- 草莓 Perl:
C:\Strawberry
Add .PL
to your PATHEXT environment variable.
添加.PL
到您的 PATHEXT 环境变量。
This makes Windows consider *.pl files to be executable when searching your
PATH. It enables you to run myscript
instead of myscript.pl
.
这使得 Windows 在搜索 PATH 时将 *.pl 文件视为可执行文件。它使您能够运行myscript
而不是myscript.pl
.
You can set it for the current cmd session
您可以为当前的 cmd 会话设置它
set PATHEXT=%PATHEXT%;.PL
To set it permanently (under Windows Vista or Windows 7)
永久设置(在 Windows Vista 或 Windows 7 下)
setx PATHEXT %PATHEXT%;.PL
Under Windows XP you have to use the GUI:
在 Windows XP 下,您必须使用 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.
- 右键单击我的电脑,然后单击属性。
- 单击高级选项卡。
- 单击环境变量。
- 选择 PATHEXT,然后单击编辑。
- 附加
;.PL
到当前值。
Make I/O redirection work
使 I/O 重定向工作
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.
I/O 重定向(例如program | myscript
)不适用于通过文件关联启动的程序。有一个注册表补丁可以纠正这个问题。
- 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.
- 启动注册表编辑器。
- 在注册表中找到并单击以下项:
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer
- 在编辑菜单上,单击添加值,然后添加以下注册表值:
- 值名称:
InheritConsoleHandles
- 数据类型:
REG_DWORD
- 基数:
Decimal
- 价值数据:
1
- 值名称:
- 退出注册表编辑器。
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.
警告:原则上,这应该只在 Windows XP 上是必要的。根据我的经验,这在 Windows 7 中也是必要的。在 Windows 10 中,这是非常有害的——程序在 stdout/stderr 上执行但不产生任何结果。注册表项需要设置为 0 而不是 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.
如果修补注册表不是一个选项,则运行program | perl -S myscript.pl
对于 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:
添加 Perl 的拖放处理程序允许您通过拖放运行 Perl 脚本;例如,将文件拖到 Windows 资源管理器中的文件图标上并将其放在那里。运行以下脚本将必要的条目添加到注册表中:
use Win32::TieRegistry;
$Registry->Delimiter("/");
$perlKey = $Registry-> {"HKEY_CLASSES_ROOT/Perl/"};
$perlKey-> {"shellex/"} = {
"DropHandler/" => {
"/" => "{86C86720-42A0-1069-A2E8-08002B30309D}"
}};
回答by Dallaylaen
Convert your perl scripts into batch files using pl2batonce they are ready to be run by users.
一旦准备好供用户运行,就使用pl2bat将您的 perl 脚本转换为批处理文件。
The trick works through the perl -x
switch which, according to perldoc perlrun
, makes Perl search for the first line looking like #!.*perl
.
这个技巧通过perl -x
开关起作用,根据perldoc perlrun
,它使 Perl 搜索看起来像 的第一行#!.*perl
。
回答by ms609
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.
按照已接受答案中的说明进行操作后,即使将 perl.exe 设置为默认文件处理程序,双击仍会导致在 Windows 10 中使用记事本打开 .pl 文件。
After finding Hyman Wu's comment at ActivePerl. .pl files no longer execute but open in Notepad insteadI was able to run perl scripts on double-click as such:
在ActivePerl 上找到 Hyman Wu 的评论后。.pl 文件不再执行,而是在记事本中打开,而是我能够双击运行 perl 脚本,如下所示:
- 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
)
- 选择并右键单击 .pl 文件
- 使用“打开方式”子菜单“选择另一个应用程序”
- 选择“始终使用此应用程序打开 .pl 文件”(现在执行此操作 - 选择程序后您将没有机会)
- 滚动到“其他选项”底部找到“更多应用”,然后选择“在这台电脑上查找其他应用”
- 导航到 C:/path/to/perl/bin/ 并选择
Perl5.16.3.exe
(或等效项,取决于您安装的 Perl 版本:但不是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.
然后 Perl 图标出现在 .pl 文件旁边,双击后每次都可以根据需要在 Perl 中打开它们。
回答by Nyvis
I tried the assoc and ftype methods and they didn't work for me.
我尝试了 assoc 和 ftype 方法,但它们对我不起作用。
What worked was editing this registry key:
有效的是编辑此注册表项:
Computer\HKEY_CURRENT_USER\Software\Classes\Applications\perl.exe\shell\open\command
Computer\HKEY_CURRENT_USER\Software\Classes\Applications\perl.exe\shell\open\command
It was set to:
"C:\Perl64\bin\perl.exe" "%1"
它被设置为:
"C:\Perl64\bin\perl.exe" "%1"
When it should be:
"C:\Perl64\bin\perl.exe" "%1" %*
什么时候应该:
"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.
它与 ftype 的内容相同,但出于神秘的窗口原因,我也必须将其设置在那里。
回答by Some Guy
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.
像其他一些人一样,我设置了“assoc”和“ftype”,但也通过 GUI 设置了记事本文本编辑器,当我尝试通过命令行执行脚本时,Windows 调用记事本来编辑脚本而不是运行我的脚本。
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).
使用 GUI 将 .pl 文件关联指向运行脚本的可执行文件并没有太大的改进,因为它会在我的脚本上调用可执行文件,但不会传递命令行参数(即使我从命令行)。
I finally found salvation herewhich 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
就我而言,按照说明使用 RegEdit 删除
HKEY_CLASSES_ROOT \ Applications \ perl.exe
HKEY_CLASSES_ROOT \ Applications \ perl.exe
where perl.exe is the name of my Perl executable, and then also deleting:
其中 perl.exe 是我的 Perl 可执行文件的名称,然后还删除:
HKEY_CLASSES_ROOT \ .pl
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.
似乎解决了我的问题,然后(在重新执行“assoc”和“ftype”命令后,如其他答案所示)然后我可以从 cmd.exe 执行脚本并让它们运行并访问其命令行参数。
Some other related information here.
这里有一些其他相关信息。