Reputation: 31
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
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:
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%'));
Throw in extra backslashes to protect the single quotes from your shell. Prepending echo
to your command could help with the debugging....
(my personal favorite) Don't shell out for your Database access. use DBI
.
Upvotes: 5
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