perlcsh

csh passing $* to Perl script not working


I am writing a shell script in csh, and one of its functions is to execute a Perl script that takes in arguments.

The csh script itself takes in command line arguments (stored as space-separated values in $*), and I pass this to the Perl script.

My issue is that when I run:

perl myscript.pl --args $*

the Perl script only gets the first argument in $*. However, if I run echo $* from within the csh script, it correctly prints out all the arguments.

There can be any number of arguments passed (thus I cannot store my arguments in static variables and pass them through).

Does anyone know why this is happening and how I can fix this?


Solution

  • You need to quote $* in your csh script:

    perl myscript.pl --args "$*"
    

    Here is myscript.pl:

    use warnings;
    use strict;
    use Getopt::Long;
    use Data::Dumper;
    
    my %opt;
    GetOptions(\%opt, 'args=s');
    print Dumper(\%opt);
    

    Here is how I call it:

    script.csh arg1 arg2
    
    $VAR1 = {
              'args' => 'arg1 arg2'
            };