my $file = 'template.txt'; my $foo = 'footext'; my $content = ''; { local $/; open my $fh, '<', $file or die $!; $content = <$fh>; } $content =~ s/\$foo/$foo/g; print $content;