Pass perl variable to sed

Hello,

I'd like to pass a variable to a sed command in a perl script. The script is like this :

#!/usr/bin/perl -w

$newline="new";
system q(sed '/insert/ i\ '$newline <sed1.txt >sed2.txt);

But the interpretor wouldn't recognize $newline, it inserts a "\n" instead.

I've also tried : system q(sed "/insert/ i\ $newline" <sed1.txt >sed2.txt);
Then I got the same result.

Anyone has an idea? Thanks.

sed -f <sed program file name>

You can write sed commands to file from perl; then execute the command file with -f

Thank you, Jim.
But then I need to call "sed -f .." in the perl script, and pass the perl variables to the sed script. It seems to complicate the problem.

I've found out that might be a concatenation problem in my system function. I rewrited it like this :
system('sed -e "/insert/ i\\'.$newline.'" <sed1.txt >sed2.txt');
And it works.

Now I have a new problem. It wouldn't let me use "+" for the regular expression. For example,
system('sed -e "/insert+/ i\\'.$newline.'" <sed1.txt >sed2.txt');
It fails to match "inserttt".
But with "*", it works well.

I have a similar issue. I also need to pass a perl variable into the sed command as follows:

system("sed -e s/",$test[0],"/",$test[1],"/g file > output");

Basically, I want to have my perl script call the sed command to search for the string in subscript 0 of the 'test' array and replace it with subscript 1 of the 'test' array. I tried hi_ryo's method, but it didn't work in my case.

If anyone is interested in answering this question, I'd like to know how to do it too.