2

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?

Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
quinekxi
  • 879
  • 5
  • 14
  • 27
  • Yes, but what I found doesn't makes sense to me. – quinekxi Nov 22 '11 at 09:56
  • 2
    `system` returns a single value, not an array. See `perldoc -f system` for a detailed description. [This thread](http://www.perlmonks.org/?node_id=421595) on perlmonks discusses the error you're getting (with a few different solutions being presented). – gamen Nov 22 '11 at 10:07

3 Answers3

5

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.

gamen
  • 987
  • 6
  • 12
2

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.";
}
aje
  • 44
  • 1
-1

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.

salva
  • 9,943
  • 4
  • 29
  • 57