Linux 在没有扩展名的 Windows 上运行 perl 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3677114/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 23:15:57  来源:igfitidea点击:

Running a perl script on windows without extension

perlexecutable

提问by Pascal

I am trying to find a way to register the files with extension .plas executables. I spent some time on the web looking for a solution, but I couldn't find anything.

我试图找到一种方法将扩展名的文件注册.pl为可执行文件。我花了一些时间在网上寻找解决方案,但找不到任何东西。

What I can do: I made a script, let's call it myscript.pl
I can run it like this :

我能做什么:我制作了一个脚本,我们称之为myscript.pl
我可以像这样运行它:

perl myscript.pl [my_script_parameters]

Now since the file is associated with perl, I can also run it as:

现在,由于该文件与 perl 相关联,我还可以将其运行为:

myscript.pl [my_script_parameters] 

Now, I know that there is somewhere a list of extensions that are considered as executables(.exe, .bat, etc…). I would like to add.plto this listso that I can run my script like this:

现在,我知道某处有一个被视为可执行文件的扩展名列表(.exe、.bat 等...)。我想添加.pl到此列表中,以便我可以像这样运行我的脚本:

myscript [my_script_parameters]

Does anyone know how to do this?

有谁知道如何做到这一点?

回答by Ed Guiness

Yes, there is built-in support for this. If you check the help for command FTYPE you will see a perl example.

是的,对此有内置支持。如果您查看命令 FTYPE 的帮助,您将看到一个 perl 示例。

C:>help ftype

C:>帮助 ftype

Displays or modifies file types used in file extension associations

FTYPE [fileType[=[openCommandString]]]

fileType Specifies the file type to examine or change openCommandString Specifies the open command to use when launching files of this type.

Type FTYPE without parameters to display the current file types that have open command strings defined. FTYPE is invoked with just a file type, it displays the current open command string for that file type. Specify nothing for the open command string and the FTYPE command will delete the open command string for the file type. Within an open command string %0 or %1 are substituted with the file name being launched through the assocation. %* gets all the parameters and %2 gets the 1st parameter, %3 the second, etc. %~n gets all the remaining parameters starting with the nth parameter, where n may be between 2 and 9, inclusive. For example:

ASSOC .pl=PerlScript
FTYPE PerlScript=perl.exe %1 %*

would allow you to invoke a Perl script as follows:

script.pl 1 2 3

If you want to eliminate the need to type the extensions, then do the following:

set PATHEXT=.pl;%PATHEXT%

and the script could be invoked as follows:

script 1 2 3

显示或修改文件扩展名关联中使用的文件类型

FTYPE [文件类型[=[openCommandString]]]

fileType 指定要检查或更改的文件类型 openCommandString 指定启动此类文件时要使用的打开命令。

键入不带参数的 FTYPE 以显示定义了打开命令字符串的当前文件类型。FTYPE 仅使用文件类型调用,它显示该文件类型的当前打开命令字符串。不为打开命令字符串指定任何内容,FTYPE 命令将删除文件类型的打开命令字符串。在打开的命令字符串中,%0 或 %1 替换为通过关联启动的文件名。%* 获取所有参数,%2 获取第一个参数,%3 获取第二个参数,等等。 %~n 获取从第 n 个参数开始的所有剩余参数,其中 n 可能介于 2 和 9 之间,包括 2 和 9。例如:

ASSOC .pl=PerlScript
FTYPE PerlScript=perl.exe %1 %*

将允许您调用 Perl 脚本,如下所示:

script.pl 1 2 3

如果您不想输入扩展名,请执行以下操作:

set PATHEXT=.pl;%PATHEXT%

并且可以按如下方式调用脚本:

script 1 2 3

回答by PP.

Your best approach would be to write a batch file called myscript.bat, place it in your path, and have it run your script.. e.g.

您最好的方法是编写一个名为 的批处理文件myscript.bat,将其放在您的路径中,并让它运行您的脚本。例如

@echo off
c:\perl\bin\perl.exe c:\scripts\myscript.pl %*

回答by AndiDog

You can simply add ";.PL" to the PATHEXT environment variable. Right-click "My computer" > Properties > Advanced > Environment variables > System variables.

您可以简单地将“;.PL”添加到 PATHEXT 环境变量中。右键单击“我的电脑”> 属性 > 高级 > 环境变量 > 系统变量。