简体   繁体   English

如何创建拖放草莓 Perl 程序?

[英]How do I create drag-and-drop Strawberry Perl programs?

I've got a Strawberry Perl program that accepts a single-file as a command-line argument.我有一个Strawberry Perl程序,它接受单个文件作为命令行参数。 How can I set things up such that I can drag and drop the desired file onto the Strawberry Perl program (or a wrapper around it) and the program runs with that file's name as an argument?如何进行设置,以便我可以将所需的文件拖放到 Strawberry Perl 程序(或它周围的包装器)上,并且程序以该文件的名称作为参数运行?

Under Windows (tested with XP), you can create a.cmd file and simply have it run the Perl program with the argument of %1 to pass the filename over, as if executed by commandline.在 Windows(用 XP 测试)下,您可以创建一个 .cmd 文件并简单地让它运行 Perl 程序,参数为 %1 以通过命令行传递文件名。

perl c:\test.pl %1

Then you can simply drag and drop a file onto the.cmd file to execute.然后您可以简单地将文件拖放到.cmd文件上执行。

Eeek.哎呀。 Please don't create a wrapper script/cmd when you don't need to.请不要在不需要时创建包装脚本/cmd。

Go into your Registry or your File Type dialog box in Windows, and redefine the Perl default action to say: Go 进入您的注册表或 Windows 中的文件类型对话框,然后重新定义 Perl 默认操作为:

"C:\path-to-perl-folders\perl.exe" "%1" %*

This will cause double-clicking the.PL to launch perl.exe with the name of the double-clicked file (%1).这将导致双击 .PL 以启动 perl.exe 并使用双击文件 (%1) 的名称。 The %* stuff (passing any filename arguments to the Perl script) is trickier. %* 东西(将任何文件名 arguments 传递给 Perl 脚本)比较棘手。

Go into the Registry again (really, it's not as scary as people think) and find/create a "shellex" key under the Perl class, and then create a sub-key called "DropHandler" with a default string value of "{86C86720-42A0-1069-A2E8-08002B30309D}" (at least, that's my DropHandler in the US version of Windows XP). Go into the Registry again (really, it's not as scary as people think) and find/create a "shellex" key under the Perl class, and then create a sub-key called "DropHandler" with a default string value of "{86C86720 -42A0-1069-A2E8-08002B30309D}”(至少,这是我在美国版 Windows XP 中的 DropHandler)。

This allows.pl files (actually, anything associated with the Perl class) to have a drop handler that tells Explorer what to do when you drop file(s) on the.pl script.这允许 .pl 文件(实际上是与 Perl 类相关联的任何内容)有一个删除处理程序,该处理程序告诉资源管理器在您在 .pl 脚本上删除文件时要做什么。 In this case, it just means "run the Perl script with the dropped file(s) as arguments".在这种情况下,它只是意味着“以删除的文件作为参数运行 Perl 脚本”。

Hmmm, I don't think I explained that very well, but that's how I've set up Perl (running off a network drive) for a large engineering organization.嗯,我不认为我解释得很好,但这就是我为大型工程组织设置 Perl(运行网络驱动器)的方式。 Google for Perl and DropHandler, and you should be able to get the.reg Registry script to do this for you.谷歌搜索 Perl 和 DropHandler,您应该可以获取 .reg 注册表脚本来为您执行此操作。

Here's another alternative to a "wrapper", but it requires a slight modification to the perl script:这是“包装器”的另一种替代方案,但需要对 perl 脚本稍作修改:

  1. Rename your script.pl script to script.cmd.将您的 script.pl 脚本重命名为 script.cmd。
  2. Add the following to the top of the file:将以下内容添加到文件顶部:

@SETLOCAL ENABLEEXTENSIONS
@c:\path\to\perl.exe -x "%~f0" %*
@exit /b %ERRORLEVEL%
#!perl
#line 6
#...perl script continues here...

The script is run like any other batch file.该脚本像任何其他批处理文件一样运行。 The first three lines basically invokes Perl on the CMD file itself ( %~f0 , which only works if CMD extensions are turned on).前三行基本上在 CMD 文件本身上调用 Perl ( %~f0 ,仅当 CMD 扩展打开时才有效)。 The -x paremeter to perl.exe tells Perl to skip everything until the #!perl line. perl.exe 的-x参数告诉 Perl 跳过所有内容,直到#!perl行。 " #line 6 " just aids in debugging. #line 6 ”只是帮助调试。

This is my preferred solution when I don't know much about the target system (and may not be able to edit the registry).当我对目标系统不太了解(并且可能无法编辑注册表)时,这是我的首选解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM