Kevin
Kevin

Reputation: 6831

Perl Escape Quotes Problem

Pardon me if this is too trivial.

I am doing an XSLT by calling system() in Perl script like this:

system("java -Xms256m -Xmx512m -jar $saxonJar -o $tmpFile $inFile $xslFile $saxonParams");

$inFile is a string that contains a relative path to the xml file that is going to be translated using XSLT.This worked fine except for those $inFile that has space in the string, for example, like "Intro to Dance.htm", then it will report a syntax error.

If this is in MS-DOS, then I can easiliy get around this problem by putting a quote around the $inFile string in the XSLT command. I did try putting escape in the above command like:

system("java -Xms256m -Xmx512m -jar $saxonJar -o $tmpFile \"$inFile\" $xslFile $saxonParams");

It does not work. Could anybody help how should I put quotes around $inFile?

Thanks.

Upvotes: 2

Views: 429

Answers (2)

RoUS
RoUS

Reputation: 1990

Is this, in fact, MS-DOS?

You said it didn't work, but not what actually happened. Did you get an error? What was it?

If this is on a Unixish system, just substitute single quotation marks:

system("java -Xms256m -Xmx512m -jar $saxonJar -o $tmpFile '$inFile' $xslFile $saxonParams");

I don't know if that will work on MS-DOS or not.

If the filename contains " or ' it becomes a little more complex, because those will need to be preserved at the shell level:

$inFile =~ s/(['"])/\\$1/g;
system("java -Xms256m -Xmx512m -jar $saxonJar -o $tmpFile \"$inFile\" $xslFile $saxonParams");

(Or something similar.)

Better still, use the multi-argument form:

system('java', '-Xms256m', '-Xmx512m', '-jar', $saxonJar, '-o', $tmpFile,
       $inFile, $xslFile, $saxonParams);

and let the interpreter and the shell figure it out.

Upvotes: 1

friedo
friedo

Reputation: 66978

You can avoid the shell (and thus shell-escaping problems) by passing your command line to system as a list instead. Try

system( 'java', '-Xms256m', '-Xmx512m', '-jar', $saxonJar, '-o', $tmpFile, $inFile, $xslFile, $saxonParams );

See the perldoc for system for more on how this works.

Upvotes: 4

Related Questions