P0w3rK!d has asked for the wisdom of the Perl Monks concerning the following question:

How would you grab $c in the following shell script?
[Perl Program] $testprog = "testprog"; $a=1; $b=2; $c="This is a test"; system($testprog $a $b $c)
...
[in Shell script "testprog"] a=$1 b=$2 c=$3 echo $a = 1 echo $b = 2 echo $c = "This"
How do I get $c to be "This is a test"? Do I use shift or something else?

Replies are listed 'Best First'.
Re: Passing Command line values from Perl to Sh via system()
by RatArsed (Monk) on Jul 17, 2001 at 19:01 UTC
    IIRC, you need to pass the parameter with quotes, as the shell will split on whitespace otherwise...

    [Perl Program] $testprog = "testprog"; $a=1; $b=2; $c="\"This is a test\""; system($testprog $a $b $c) ... [in Shell script "testprog"] a=$1 b=$2 c=$3 echo $a = 1 echo $b = 2 echo $c = "This is a test"

    --
    RatArsed

Re: Passing Command line values from Perl to Sh via system()
by Jonathan (Curate) on Jul 17, 2001 at 19:25 UTC
    Try
    system("$testprog $a $b '$c'");
    Instead, this gives
    1 = 1 2 = 2 "This is a test" = This is a test
    Is this what you meant?
      What can I do in this case?
      #...call to function &SomeFunction($a, $b, $c); #...in the function @args = ("$program $a $b $c"); system(@args) == 0 or die "system @args failed: $?";
      ...Do I change it to this?
      #...call to function &SomeFunction($a, $b, '$c'); #...in the function @args = ("$program $a $b '$c'"); system(@args) == 0 or die "system @args failed: $?";
      ?
Re: Passing Command line values from Perl to Sh via system()
by merlyn (Sage) on Jul 17, 2001 at 20:11 UTC