简体   繁体   English

Perl Strawberry:如何通过单击运行Perl脚本

[英]Perl Strawberry: How to run a perl script by clicking

I'm working on Windows 7 and I have installed Strawberry. 我正在使用Windows 7,并且已经安装了Strawberry。 I would like to run a perl skript (test.pl): 我想运行一个perl skript(test.pl):

open OUTPUT, ">test.txt";
print OUTPUT "This is a test\n";

by just clicking on the file or redirect with left mouse click to Perl-program (open with/perl.exe). 只需单击文件或用鼠标左键重定向到Perl程序(通​​过/perl.exe打开)。 When I do this a console opens for less than a second and disappears but the file test.txt is not created. 当我这样做时,控制台会打开少于一秒钟,然后消失,但不会创建文件test.txt。 If I go to the MS command and enter 如果我转到MS命令并输入

> C:\myplace>perl test.pl  

it works. 有用。 I never had this experience before (WinXP, other Windows 7 PC with ActivePerl and Windows 8 with strawberry). 我以前从未有过这种经验(WinXP,其他带有ActivePerl的Windows 7 PC和带有草莓的Windows 8)。 I would be very happy if somebody could give me a hint how to solve this problem. 如果有人可以给我提示如何解决这个问题,我将非常高兴。

There are two problems here: 这里有两个问题:

  1. Creating the file where you want it. 在所需的位置创建文件。 When double-clicking a perl script to launch it, it is not executed in the context of the folder you have opened in Explorer. 双击一个perl脚本启动它时,它不会在资源管理器中打开的文件夹的上下文中执行。 If you want to specify an explicit context, do the following near the top of your script: 如果要指定显式上下文,请在脚本顶部附近执行以下操作:

     use FindBin; # This is a module that finds the location of your script BEGIN { chdir $FindBin::Bin } # set context to that directory. 

    When you then create a new file without an aboslute path, the path is considered relative to that directory. 然后,当您创建一个没有文件路径的新文件时,该路径被认为是相对于该目录的。

    You do not have the problem when running the script from the command line, because you have specified the correct path. 从命令行运行脚本时,您没有问题,因为您指定了正确的路径。 But if you run it from C:\\ like 但是,如果您从C:\\运行,就像

     C:\\> perl myplace/test.pl 

    then you have created the file in C\\test.txt . 那么您已经在C\\test.txt创建了文件。 The FindBin solution fixes this. FindBin解决方案可以解决此问题。

  2. When running a script by double-clicking it, the command line exits before you can inspect the output. 通过双击运行脚本时,将退出命令行,然后才能检查输出。 This “problem” is shared by all programming languages on Windows. Windows上的所有编程语言都共享此“问题”。 You can force the window to stay open by waiting for some input. 您可以通过等待一些输入来强制窗口保持打开状态。 You can either do 你可以做

     system("PAUSE"); # not portable to non-Windows! 

    or 要么

     warn "Press enter to exit...\\n"; <STDIN>; # Read a line from the command line and discard it. # Feels akward when launching from the command line 

    to wait until an Enter ⏎ is pressed . 等待直到按下Enter键。

    The other solution is to always use the command line for your scripts, which is what I'd actually suggest. 另一种解决方案是始终对您的脚本使用命令行,这是我实际上建议的。

Check what is your script executing folder, as it might differ from C:\\myplace 检查您的脚本执行文件夹是什么,因为它可能与C:\\myplace

use Cwd;
print getcwd();
sleep 7;

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

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