4

I have a perl script test.pl which reads arguments from the command line

#!/usr/bin/perl -w  
for( @ARGV ) { print( "$_\n" ) } ;

This works well when I pass parameters like "a b" "c d" e on the command line
The result is:

a b
c d
e

If I declare a variable

X='"a b" "c d" e'

and then run

test.pl $X

I get

"a
b"
"c
d"
e

I need to run the perl script from various shell scripts as a helper function and the parameters are calculated by those scripts.
The parameter count is not fixed so I have to pass it as a list.
Alas I cannot find a way to get the perl program handle my parameters as desired.
I have thought of passing the parameters through file but then the manual invocation of the perl script from the command line becomes awkward.
How can I get the perl script preserve the spaces in the paramters?

4
  • 1
    Arguments passed in perl script gets saved in @ARGV array So when you are printing it gives values from that array and printing it as different arguments given because It does not preserving the space between your arguments, Space is used to identify the different arguments. Commented Dec 20, 2016 at 12:32
  • Possible duplicate of Perl script that has command line arguments with spaces Commented Dec 20, 2016 at 12:37
  • passing arguments on the command line works OK as I have described above. What doesn't work is to pass arguments throug a variable. Commented Dec 20, 2016 at 12:44
  • passing arguments on the command line works OK as I have described above. so it is not a duplicate of the above question. Commented Dec 20, 2016 at 12:51

3 Answers 3

2

If you're using bash you might consider using an array variable to hold the arguments to prevent the loss of information when the variable is substituted into the test.pl command invocation:

declare -a X=("a b" "c d" e)
test.pl "${X[@]}"

If it helps, you can build up the array piece-by-piece:

X=("a b")
X+=("c d")
X+=("e")
Sign up to request clarification or add additional context in comments.

2 Comments

I tried the array with bash and it works. Unfortunately I need to use ksh for backwards compatibility and it doesn't work there. So I am looking for an easy way to get my perl program distinguish between input from STDIN which I would use to invoke it from ksh scripts and input from command line parameters which I then would use for interactive invocations.
Which version of Korn shell are you using? Might be worth adding details to the question.
2

The problem is the shell. executing

./test.pl "$X"

will give one argument to the shell. One solution would be to use

/bin/bash -c "./test.pl $X"

HTH Georg

2 Comments

I think you have a syntax error, Don't you mean ./test.pl "$X" or /usr/bin/perl test.pl "$x".
You are absolutely right. Otherwise this only works when test.pl is in the PATH i guess
0

Thanks @all for the hints. Parameter passing from shells seems to be not as easy as I had thought. Thus I have added another parameter to my perl program, which tells it to read the parameters from a file instead of the command line. This works like desired and circumvents shell interpretations.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.