Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access paths with spaces in them in Perl on Windows?

Tags:

windows

perl

I'm converting a Unix Perl script to run on Windows. I'm having a problem with paths that have spaces in them:

open (IN, "| C:\\Program Files\\MySQL\\MySQL Server 5.1\\bin\\mysql.exe -u root -ppwd") or die "$!";

The code above throws the following error:

'C:\Program' is not recognized as an internal or external command,

I tried wrapping in escaped \" like this:

open (IN, "| \"C:\\Program Files\\MySQL\\MySQL Server 5.1\\bin\\mysql.exe -u root -ppwd\"") or die "$!";

But no joy. How do I handle paths with spaces?

I'm using ActiveState v5.10.0 built for MSWin32-x86-multi-thread.

like image 844
Kev Avatar asked Aug 31 '25 16:08

Kev


2 Answers

You are quoting the entire command, including the command-line arguments. You should have put your second escaped quote after the mysql.exe:

open (IN, "| \"C:\\Program Files\\MySQL\\MySQL Server 5.1\\bin\\mysql.exe\" -u root -ppwd") or die "$!";

You might also be interested in the qq() and q() operators, which allow you to use delimiters other than quotation marks to delimit strings. They are very helpful when you want to quote a string that includes quotes:

qq[| "C:\\Program Files\\MySQL\\MySQL Server 5.1\\bin\\mysql.exe" -u root -ppwd]

Also, Perl will happily handle the correct path separator for command names (but not always for command arguments, so beware):

qq[| "C:/Program Files/MySQL/MySQL Server 5.1/bin/mysql.exe" -u root -ppwd]

(And since this example doesn't need any interpolation, you could have used single-quotes or the q() construction:

'| "C:\\Program Files\\MySQL\\MySQL Server 5.1\\bin\\mysql.exe" -u root -ppwd'

)

like image 110
mob Avatar answered Sep 03 '25 14:09

mob


It's Perl, so there are a 1000 ways (as you'll see), One way (escape the spaces)

open (IN, "| C:\\Program\ Files\\MySQL\\MySQL\ Server\ 5.1\\bin\\mysql.exe -u root -ppwd") or die "$!";
like image 44
Lou Franco Avatar answered Sep 03 '25 12:09

Lou Franco