Perl system call does not recognize paths

I am trying to execute a system command from a Perl program.

It works great if I did not provide a path when it says: "The system cannot find the path specified."

I get the same results with exec (), system () or backticks.

I get the same results with the command line directly as the argument, or put it in a string with one or two quotes and pass the string as an argument.

If I copy a non-working command from a perl script and paste it into the DOS field, it will work and vice versa.

For instance,

print `cd`; 

works fine but

 print `cd \`; 

and

 print `cd ..`; 

will give me the same error message.

 $cmd = 'foo.htm'; $ret=`$cmd` 

launches the browser but

 $cmd = '\foo.htm'; $ret=`$cmd`; 

no.

Does anyone have any suggestions regarding the problem?

+4
source share
1 answer

It would be helpful if you would give us what your system is a team and what you get. It's a little hard to say what your mistake is. However, I will assume ..

If you are running Windows and you are doing \ , you should understand that the backslash character is a special quote character in Perl. To use a real backslash, you need to double it:

  system ("C:\\Program Files (x86)"\\Microsoft Office\\Word.exe"); 

Or, even better, use the File :: Spec module that ships with Perl. This ensures that you create the correct path structure:

 use File::Spec::Functions; my $executable = catfile("C:", "Program Files (X86)", "Microsoft Office", "Word.exe"); system ($executable); 

Of course, you should try to capture the output of the system command to see if there is any error:

 my $error = system($executable); if ($error) { if ($? == -1) { print "Program failed to execute\n"; } else { my $signal = ($? & 127); my $exit_code = ($? >> 8); print "Error: Signal = $signal Exit Code = $exit_code\n"; } } 
+3
source

All Articles