Perl provides for a variety of quote-ing operators for just such a scenario as you seem to be having. In particular, the qw//, qq//, and q// operators, standing for quote word, double quote string, and single quote string operators respectively. You will notice that they completely take away the drudgery out quoting data.
And as has already been pointed out, the system() command is better served using arguments served via a list rather than in scalars for security reasons.
Finally, inside the cshell code, you need to use the :q modifier to quote the variable.
% cat PerlScript.pl
my @Args = (
qw/ -stuff /, # whitespace ignored
qw/ -more_stuff /, # whitespace ignored
q['-even more "*stuff" -last_stuff'], # whitespace IMPORTANT
);
my $csh_script = q[Script.csh];
system($csh_script, @Args) and
die qq[Error: The script $csh_script has failed with exit code:], $? >> 8;
% cat Script.csh
foreach arg ( $argv[*]:q )
echo "<$arg>"
end
Results
<-stuff>
<-more_stuff>
<'-even more "*stuff" -last_stuff'>
Notes
- If you don't think the output is to your liking, then DONOT change anything inthe csh script. Rather play with
@Args in the PerlScript.pl file.
- For example, if you were to populate the
@Args like as shown:
my @Args = (
qw/ -stuff /,
qw/ -more_stuff /,
q['-even more],
q["*stuff"],
q[-last_stuff'],
#q['-even more "*stuff" -last_stuff'],
);
Then you would see the following result:
<-stuff>
<-more_stuff>
<'-even more>
<"*stuff">
<-last_stuff'>
system()rather than a scalar string. e.g.@cmdlineargs=("-stuff", "-more_stuff", "'-even_more \"*stuff\"-last_stuff'"); system('script.sh', @cmdlineargs). seeperldoc -f systemfor details.csh(andtcsh) is just barely adequate as an interactive shell. it's absolutely awful and should be avoided entirely as a scripting shell. this has useful/interesting stuff too: stackoverflow.com/questions/4317247/…