问题
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