如何使我的Perl脚本像Windows上的普通程序一样运行? [英] How do I make my Perl scripts act like normal programs on Windows?
问题描述
我希望我的Perl脚本的行为与任何其他可执行文件(* .exe文件)一样.
I want my Perl scripts to behave just like any other executable (*.exe file).
- 当我双击
myscript.pl
时,我希望它执行而不是在文本编辑器中打开. - 我想运行
myscript.pl
而不是perl myscript.pl
. - 我真的想运行
myscript
而不是myscript.pl
. - 我想运行
program | myscript
而不是program | perl myscript.pl
. - 我希望能够通过拖放操作来运行我的脚本放下.
- 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.
您必须在Windows上进行许多更改才能进行所有 这些东西起作用.用户通常会偶然发现不可以在其中完成的一项工作 时间让他们困惑自己是否犯了一个错误,其中有一个错误 Perl,Windows中存在一个错误,或者根本无法实现他们想要的行为. 该问题旨在提供一个单一的参考点 一切都在前面进行;理想情况是甚至在这些问题发生之前.
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.
相关问题:
- 我如何使Perl脚本识别Win32 cmd控制台中的参数?
- 在没有扩展名的Windows上运行Perl脚本
- 从命令行问题执行Perl
- 如何在Perl上读取Perl中的管道输入Windows?
- Windows上的Perl,文件关联和I/O重定向
- 如何创建拖放草莓Perl程序?
- 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?
推荐答案
注意:以下操作需要管理权限.为了 使用命令提示符的步骤,必须通过运行方式为 Windows Vista/Windows 7上的管理员".
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.
在shell提示符下运行以下命令:
Run the following commands at a shell prompt:
assoc .pl=PerlScript
ftype PerlScript=C:\bin\perl.exe "%1" %*
将C:\Perl\bin\perl.exe
替换为您的Perl安装路径.这
使您可以运行myscript.pl
而不是perl myscript.pl
.
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
.
默认安装位置为:
- ActivePerl:
C:\Perl
- 草莓Perl:
C:\Strawberry
- ActivePerl:
C:\Perl
- Strawberry Perl:
C:\Strawberry
这使Windows在搜索您的* .pl文件时将其视为可执行文件.
小路.它使您可以运行myscript
而不是myscript.pl
.
This makes Windows consider *.pl files to be executable when searching your
PATH. It enables you to run myscript
instead of myscript.pl
.
您可以将其设置为当前的cmd会话
You can set it for the current cmd session
set PATHEXT=%PATHEXT%;.PL
要永久设置(在Windows Vista或Windows 7下)
To set it permanently (under Windows Vista or Windows 7)
setx PATHEXT %PATHEXT%;.PL
在Windows XP下,您必须使用GUI:
Under Windows XP you have to use the GUI:
- 右键单击我的电脑",然后单击属性".
- 单击高级"选项卡.
- 单击环境变量.
- 选择PATHEXT,然后单击编辑".
- 将
;.PL
附加到当前值.
- 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.
进行I/O重定向工作
I/O重定向(例如program | myscript
)不适用于已启动的程序
通过文件关联.有一个注册表修补程序可以解决此问题.
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.
- 启动注册表编辑器.
- 在注册表中找到然后单击下面的项:
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer
- 在编辑"菜单上,单击添加值",然后添加以下注册表值:
- 值名称:
InheritConsoleHandles
- 数据类型:
REG_DWORD
- 基数:
Decimal
- 值数据:
1
- 值名称:
- 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:
警告::原则上,仅在Windows XP上才需要这样做.以我的经验,在Windows 7中也是有必要的.在Windows 10中,这是有害的-程序执行但在stdout/stderr上什么也不产生.注册表项需要设置为0而不是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.
另请参阅:
- STDIN/STDOUT Redirection May Not Work If Started from a File Association
- Perl Scripts on Windows 10 run from Explorer but not Command Prompt
如果不修补注册表,则不能运行program | perl -S myscript.pl
对于您的PATH中的脚本而言,这是一个比较烦人的解决方法.
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.
为Perl添加拖放处理程序可让您通过拖放操作运行Perl脚本.降低; 例如将文件拖放到Windows资源管理器中的文件图标上 那里.运行以下脚本,将必要的条目添加到注册表中:
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}"
}};
这篇关于如何使我的Perl脚本像Windows上的普通程序一样运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!