How to run an executable file using Perl on Windows XP?

我只是一个虾纸丫 提交于 2019-12-02 06:52:31

问题


How to run an executable file using perl?

For instance, i want to run a plain notepad.exe. How could I achieve this?

This is what I've got:

my @args = system("notepad.exe");
system(@args) == 0  or die "system @args failed: $?";

But it returns:

Can't spawn "cmd.exe": No such file or directory blah blah blah.

What am I missing?


回答1:


Try this.

my $prog = "C:\\strawberry\\perltest\\Extractor.bat";

if (-f $prog)   # does it exist?
{
    print "Will run notepad";
system($prog);
}
else  
{
    print "$prog doesn't exist.";
}



回答2:


Your code seems a bit confused. What you probably want is something like

my $cmd = "notepad.exe";
my @args = ($cmd, "readme.txt");

system(@args);

if($? == -1) {
    die "system @args failed: $?";
}

system returns a single value, not an array. See perldoc -f system for a detailed description.

This thread on perlmonks discusses the error you're getting with a few different solutions being presented.

This answer is an extension of my original comment. Sorry if it's superfluous.




回答3:


This is a Perl internal error probably caused by a broken environment. Perl can't find the Windows shell cmd.exe that is used under the hood to run the program passed to system.

Use some utility as Process Monitor to see what's going on at the OS level.



来源:https://stackoverflow.com/questions/8224839/how-to-run-an-executable-file-using-perl-on-windows-xp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!