Mary Jane
Mary Jane

Reputation: 31

Single quotes and double quotes in perl

I'm a newbie in perl and I'm trying to execute an operating system command from a Perl script.

The operating system command is executed with IPC::Open3, and the command is like

$cmd = "mycommand --add \"condition LIKE '%TEXT%' \"";

This command is supposed to insert the string contained after the "add" into a database.

The problem is that it inserts the record in the database without the single quotes around %TEXT%, like this:

condition LIKE %TEXT%

If I execute the command at a command prompt it inserts the string correctly.

How should I format the double and single quotes so that it is inserted in the database correctly?

Thanks for the help

Upvotes: 2

Views: 848

Answers (2)

tjd
tjd

Reputation: 4104

By putting the command in a single string, you are inflicting upon it a pass through your system's shell. (You don't mention if it's cmd.exe or bash or other fun stuff.)

Suggestions:

  1. Creating your system command as an array of strings will avoid the shell re-interpolating your command line. @cmd = ('mycommand', '--add', q(condition LIKE '%TEXT%'));

  2. Throw in extra backslashes to protect the single quotes from your shell. Prepending echo to your command could help with the debugging....

  3. (my personal favorite) Don't shell out for your Database access. use DBI.

Upvotes: 5

user966588
user966588

Reputation:

$cmd = q{mycommand --add "condition LIKE '%TEXT%'"};

qq is for double-quoting , q is for single quoting.

This way it takes whole command as it is.

Upvotes: 0

Related Questions