如何避免等待并关闭某些 Perl 驱动的软件?
我有一个充满脚本文件的文件夹。当我运行它们时,会打开一个它们原生的程序,它会执行一些操作并生成一个 CSV 文件。我编写了一些代码,希望运行每个脚本文件并生成一堆 CSV 文件,每个脚本一个。
发生的情况如下:当执行我的 Perl 应用程序时,软件启动并且第一个脚本成功运行(创建了一个 CSV 文件)。然而,在这一步,Perl 应用程序等待我关闭软件,然后再继续。它对每个脚本都执行此操作。我该怎么做才能避免这种情况发生?
use strict;
use warnings;
use Cwd;
my $dir = cwd();
opendir(DIR, $dir);
my @files= grep(/\.acs$/,readdir(DIR));
$dir=~s/\//\\/g;
chdir $dir;
foreach (@files)
{
print "$_\n";
system ("$_")
}
I have a folder full of script files. When I run them, a program to which they are native is opened, it does some stuff and a CSV file is generated. I wrote some code that I want to run each script file and produce a bunch of CSV files, one for each script.
What happens is the following: when my Perl application is executed, the software is launched and the first of the scripts is run successfully (a CSV file is created). However, at this step the Perl application waits for me to close the software before I continue. It does this for every script. What can I do to avoid this from happening?
use strict;
use warnings;
use Cwd;
my $dir = cwd();
opendir(DIR, $dir);
my @files= grep(/\.acs$/,readdir(DIR));
$dir=~s/\//\\/g;
chdir $dir;
foreach (@files)
{
print "$_\n";
system ("$_")
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您会想要 fork、exec 和 waitpid - 即设置并运行您自己的进程并等待它自行完成。
http://larc.ee.nthu.edu.tw/ ~cthuang/courses/ee2320/12_process.html
这并不容易,但不幸的是,您是在不支持脚本的操作系统上执行此操作。在 Linux 或 OS X 下执行此操作不会遇到任何这样的麻烦。
您需要弄清楚这些命令在 Windows 下是否可用。如果没有 posix 兼容库的话,你可能得去找一些 windows 下可用的类似的东西。
I think you'll want to fork and exec and waitpid - i.e. set up and run your own process and wait for it to finish on your own.
http://larc.ee.nthu.edu.tw/~cthuang/courses/ee2320/12_process.html
This isn't easy, but unfortunately, you're doing this on an OS that isn't script-friendly. Doing this under Linux or OS X you wouldn't have any troubles like this.
You'll need to figure out if these commands are even available under Windows. You may have to find some similar things that are available under windows if there is no posix compatibility library.
您最好的选择是让应用程序在完成后自行关闭。
例如,cmd.exe 命令的参数 /C 正是执行此操作。
尝试使用 /? 运行应用程序,看看是否出现任何有用的内容。
如果失败,您可以使用 Win32::Process 创建进程,然后在确定完成后将其终止。请参阅相关文档。
your best choice is to ask the application nicely to close itself after it is done.
for example, the cmd.exe command have parameter /C that does exactly that.
try to run the application with /?, and see if anything useful comes up.
failing that, you can use Win32::Process to create the process and then kill it after you are sure it is done. see the documentation for that.