How can I disable variable interpolation using the Perl replacement operator?

I am trying to replace a specific line in a text file with VMS. This is usually a simple single line with Perl. But I ran into a problem when the spare side was a character containing a VMS path. Here is the file and what I tried:

The contents of file1.txt:

foo
bar
baz
quux

Trying to replace the third line:

$ mysub = "disk$data1:[path.to]file.txt"
$ perl -pe "s/baz/''mysub'/" file1.txt

Sets the following output:

foo
bar
disk:[path.to]file.txt
quux

It seems that Perl was an overeager and replaced part of the $data1path with the contents of a nonexistent variable (i.e. nothing). This works with a debugger. I did not supply /e, so I thought Perl should just replace the text as it is. Is there a way to get Perl to do this?

(Also note that I can reproduce similar behavior on the linux command line.)

+5
8

:

"string" + "substitute symbol" + "string". . ... .

$ perl -pe "s'baz'"'mysub'"'" file1.txt

DCL.

Perl q() . parens, DCL. , () .

$ perl -pe "s/baz/q(''mysub')/e" file1.txt
+4

ruakh , perl %ENV, :

perl -pwe "s/baz/$ENV{mysub}/" file1.txt

-w, , warnings .

+4

VMS , ... ?

$ mysub = "disk\$data1:[path.to]file.txt"

, ,

$ mysub = "disk\\$data1:[path.to]file.txt"

?

+1

sh

perl -pe'BEGIN { $r = shift(@ARGV) } s/baz/$r/' "$mysub" file1.txt

- mysub Perl. . .

OP:

, . VMS

perl -pe "BEGIN { $r = shift(@ARGV) } s/baz/$r/" 'mysub' file1.txt
+1

sed.

Sed $data1 , , .

$ sed -e "s/baz/''mysub'/" file1.txt
0

You need to avoid $using \$. Otherwise, Perl sees the variable reference and replaces the string with the $data1contents $data1before the regular expression is evaluated. As you have not defined $data1, it is, of course, empty.

0
source

You can use the following code:

$ mysub='disk\$data1:[path.to]file.txt'
$ perl -pe 's/baz/'$mysub'/' FILE
foo
bar
disk$data1:[path.to]file.txt
quux
0
source

You can try using a logical name instead of the DCL character:

$ define mysub "disk$data1:[path.to]file.txt"
$ $ perl -pe "s/baz/mysub/" file1.txt
0
source

All Articles