I have a Perl script that processes a bunch of file names, and uses those file names inside backticks. But the file names contain spaces, apostrophes and other funky charact
Are you looking for quotemeta?
Returns the value of EXPR with all non-"word" characters backslashed.
Update: As hobbs points out in the comments, quotemeta
is not intended for this purpose and upon thinking a little more about it, might have problems with embedded nul
s. On the other hand String::ShellQuote croaks upon encountering embedded null
s.
The safest way is to avoid the shell entirely. Using the list form of 'system' can go a long way towards that (I found out to my dismay a few months ago that cmd.exe
might still get involved on Windows), I would recommend that.
If you need the output of the command, you are best off (safety-wise) opening a pipe yourself as shown in hobbs' answer