如何让我的 Perl 脚本像 Windows 上的普通程序一样运行? [英] How do I make my Perl scripts act like normal programs on Windows?

查看:36
本文介绍了如何让我的 Perl 脚本像 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.
  • 我想运行程序|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 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.

您必须在 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.

相关问题:

推荐答案

注意:以下操作需要管理权限.为了使用命令提示符的步骤必须通过运行方式"启动管理员"在 Windows Vista/Windows 7 上.

在 shell 提示符下运行以下命令:

Run the following commands at a shell prompt:

assoc .pl=PerlScript
ftype PerlScript=C:inperl.exe "%1" %*

C:Perlinperl.exe 替换为 Perl 安装路径.这使您能够运行 myscript.pl 而不是 perl myscript.pl.

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

默认安装位置是:

  • 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:

  1. 右键单击我的电脑",然后单击属性".
  2. 点击高级标签.
  3. 点击环境变量.
  4. 选择 PATHEXT,然后单击编辑.
  5. ;.PL 附加到当前值.
  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.

使 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.

  1. 启动注册表编辑器.
  2. 在注册表中找到并单击以下项:HKEY_LOCAL_MACHINESoftwareMicrosoftWindowsCurrentVersionPoliciesExplorer
  3. 在编辑菜单上,单击添加值,然后添加以下注册表值:
    • 值名称:InheritConsoleHandles
    • 数据类型:REG_DWORD
    • 基数:十进制
    • 值数据:1

警告:原则上,这应该只在 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.

另见:

如果修补注册表不是运行 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆