erwos has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to run a command via system() that has curly braces in its argument. It looks something like this:

`some_command a={1,2},{3,4}`;

What the system apparently tries to execute is:

some_command a=1,2,3,4

That is to say, no curly braces get included. I don't have the option to use anything but curly braces, and I cannot use whitespace to separate out the curly braces at all. Using a literal string ('') in a proper system() command doesn't fix it, either.

Does anyone have a solution or workaround? I've been wrestling with this one for a bit, but haven't even found anything on Google.

Thanks!

Replies are listed 'Best First'.
Re: System commands and curly braces
by ikegami (Patriarch) on Aug 10, 2010 at 18:49 UTC

    You're invoking the shell, and those characters have special meaning to your shell.

    $ echo some_command a={1,2},{3,4} some_command a=1,3 a=1,4 a=2,3 a=2,4

    You need to pass a shell literal that produces the desired arg, not the arg itself.

    $ echo some_command 'a={1,2},{3,4}' some_command a={1,2},{3,4}
    sub text_to_sh_literal { my ($s) = @_; s/'/'\\''/g; return "'$s'"; } my $arg_lit = text_to_sh_literal($arg); `some_command $arg_lit`

    You could also avoid the shell.

    open(my $fh, '-|', 'some_command', $arg); <$fh>
Re: System commands and curly braces
by toolic (Bishop) on Aug 10, 2010 at 17:45 UTC
    Try to escape the curly braces:
    `some_command a=\{1,2\},\{3,4\}`;

    Here is a complete example (on linux), using system instead of backticks to show the output:

    $ cat foo.pl #!/usr/bin/env perl use strict; use warnings; system './some_command a=\{1,2\},\{3,4\}'; $ $ cat some_command #!/usr/bin/env perl use strict; use warnings; use Data::Dumper; print Dumper(\@ARGV); $ $ ./foo.pl $VAR1 = [ 'a={1,2},{3,4}' ]; $