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

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.
  • Comment on Re^2: passing parameters from shell script to perl program doesn't preserve spaces

Replies are listed 'Best First'.
Re^3: passing parameters from shell script to perl program doesn't preserve spaces
by haukex (Archbishop) on Dec 20, 2016 at 13:36 UTC

    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