M
M
Mavven2019-03-20 11:55:17
Perl
Mavven, 2019-03-20 11:55:17

How to pass the value of a variable in perl to an external program?

Can you please tell me how to correctly pass the value of $ word to system () for further execution in the command?
I tried options with different quotes, escaping. I can not figure it out, please tell me how the value is correctly transferred.

#!/usr/bin/perl
my $word = 'test';
#my $file = '/home/text.txt'
my @cmd = q[echo "$word" >> "/home/text.txt"];
system(@cmd);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konvergent, 2019-04-23
@Mavven

Try like this:

#!/usr/bin/perl
my $word = 'test';
#my $file = '/home/text.txt'
my @cmd = qq(echo "$word" >> "/home/text.txt");

system(@cmd);

q() is single quote wrapping without interpolation,
qq() is double quotes with interpolation (i.e. substitution of variable values)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question