Getopt::Long getting a string with spaces into a variable

后端 未结 4 1189
旧时难觅i
旧时难觅i 2021-01-15 09:42

I\'m making a perl script which uses Getopt::Long to parse command line arguments. However, I have an argument which can accept a string (with spaces). How can I get the who

4条回答
  •  春和景丽
    2021-01-15 10:19

    Now riddle me this... in the following scenario, I want to generate a quoted command-line argument via a shell command which then gets passed to a Perl script, like this:

    script.pl `echo --string \"blah blah yup\"`
    

    Trouble is, GetOptions ("string=s" => \$string) sets string's value to

    "blah
    

    containing the initial double-quote character and breaking at the first whitespace. The echo command by itself outputs the parameter with a quoted string that looks peachy:

    --string "blah blah yup"
    

    and if I call script.pl --string "blah blah yup" without the echo, the whitespace containing string gets set in the Perl script as expected.

    I thought that maybe the backticks were the issue, but the same results ensue with these variants:

    script.pl $(echo --string \"blah blah yup\")
    script.pl $(echo "--string \"blah blah yup\"")
    script.pl $(echo --string \"blah\ blah\ yup\")
    script.pl $(echo '--string \"blah blah yup\"')
    

    though that last example leads to string=\"blah (containing the initial backslash and double quote character).

    Seems like the output returned from a shell command's STDOUT is not being handled the same way by Perl's argument parsing machinery as a string that is typed into the command-line. This apparent lack of string fungibility is somewhat surprising, but probably owing to some aspect of bash I don't understand. (Btw, I tested this with Perl 5.24 on Darwin and 5.16 on Linux)

提交回复
热议问题