in reply to passing parameters from shell script to perl program doesn't preserve spaces [solved]

Order of shell expansions is important here. For example, in bash, word splitting happens after parameter expansion, so X='"a b"' is split into two words, "a and b" . There's no easy and secure way to prevent it.

What shell are you using? If it's bash, you can use an array to hold the parameters:

#! /bin/bash X=( 'a b' 'c d' e ) test.pl "${X[@]}"

Update: Inserted the 1st paragraph.

($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Replies are listed 'Best First'.
Re^2: passing parameters from shell script to perl program doesn't preserve spaces
by Discipulus (Canon) on Dec 20, 2016 at 13:00 UTC
    Confirmed, i was trying something similar:

    /usr/bin/bash$ aa=('a b' 'c d' e);perl -e 'print qq(>$_<\n) for @ARGV' + "${aa[@]}" >a b< >c d< >e<

    L*

    Edit: removed -l perl switch which caused more newlines

    PS: now i remember why I choosed Perl!

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re^2: passing parameters from shell script to perl program doesn't preserve spaces
by peterbk (Initiate) on Dec 20, 2016 at 13:30 UTC
    In fact the shell scripts are mostly ksh scripts due to backward compatibility with older HP-UX systems.
    I have tried with bash but the problem is the same.
    Is there an easy way of distinguishing in perl between input from STDIN and using parameters?
    If so I would go and pass arguments from scripts through STDIN and arguments from interactive shells as parameters.

      Hi peterbk,

      Is there an easy way of distinguishing in perl between input from STDIN and using parameters?

      For determining whether the script should use STDIN for accepting parameters, you could use the usual convention (at least for many *NIX tools) that the script has no arguments passed to it, or a single argument consisting of a dash:

      my @args; if (!@ARGV || @ARGV==1 && $ARGV[0] eq '-') { @args = <STDIN> } else { @args = @ARGV }

      Update: This doesn't handle quoting on the arguments passed on STDIN, but it does allow whitespace in arguments if you place the arguments each on their own lines. If you really need to parse quotes, Text::ParseWords's shellwords has already been mentioned.

      Hope this helps,
      -- Hauke D