Hi peterbk,

It is not Perl which decides how to split the command line arguments, it is the shell. Which shell are you using? I'm not an expert on the intricacies of shell quoting, but from what I do know it can be a minefield of brittle/non-portable solutions.

I would recommend finding a different solution to passing arguments to your script, such as allowing your script to get options from a file in addition to accepting ones from the command line (I don't find that awkward). Also, note that Perl can access environment variables via the %ENV hash, but if those contain quotes, you will have to parse those yourself, such as with shellwords from Text::ParseWords, as Corion suggested.

Using a file to pass parameters gives you a lot of flexibility on formats, which all handle the quoting/escaping of values too (JSON, YAML, XML, any of the Config:: modules, etc.).

Here's a little bash trick I sometimes use for temporary files that get cleaned up when the script ends, you could use this to generate the file which holds the values:

# in your bash script TEMPFILE="`mktemp`" trap 'rm -f "$TEMPFILE"' EXIT echo "whatever" >$TEMPFILE # ...

Update: If your Perl script happens to be using Getopt::Long, note that it has the function GetOptionsFromString, and if you're using Getopt::Std, see my node here. Both of these solutions use shellwords from Text::ParseWords.

Hope this helps,
-- Hauke D


In reply to Re: passing parameters from shell script to perl program doesn't preserve spaces by haukex
in thread passing parameters from shell script to perl program doesn't preserve spaces [solved] by peterbk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.