I'm trying to replace a particular line in a text file on VMS. Normally, this is a simple one-liner with Perl. But I ran into a problem when the replacement side was a symbol containing a VMS path. Here is the file and what I tried:
Contents of file1.txt:
foo
bar
baz
quux
Attempt to substitute 3rd line:
$ mysub = "disk$data1:[path.to]file.txt"
$ perl -pe "s/baz/''mysub'/" file1.txt
Yields the following output:
foo
bar
disk:[path.to]file.txt
quux
It looks like Perl was overeager and replaced the $data1
part of the path with the contents of a non-existent variable (i.e., nothing). Running with the debugger confirmed that. I didn't supply /e
, so I thought Perl should just replace the text as-is. Is there a way to get Perl to do that?
(Also note that I can reproduce similar behavior at the linux command line.)
s///
by using the delimiter'
instead of/
(e.g.s'baz'disk$data1:[path.to]file.txt'
). Unfortunately, judging from what you've posted, it looks like VMS uses'
to perform its own variable substitution? So, it might be easier-said-than-done to get the single-quotes into the Perl script. :-/ – Infancy''
and one'
to do variable substitution on VMS, and its interpreter is easily confused. – Nollieuse strict; use warnings;
If you had definedmy $mysub = 'disk$data1:[path.to]file.txt'
(note the single quotes to avoid variable interpolation) or evenmy $mysub = $ENV{mysub]
(if you insist on using shell variables) inside a perl script, you would not have this problem. – Gallant$ENV{mysub}
would completely address the problem; that's the minimal fix. Instead of trying to disable the interpolation in Perl, the OP should disable the interpolation in VMS, by writingperl -pe "s/baz/$ENV{mysub}/" file1.txt
! – Infancymysub
would have to be defined as a logical in that case, but yep, I think you're right. – Nollie