in reply to Adding <pre> at beginning </pre> at file end

You can do that with a regular expression, but there's an easier way. You can just open your new file, write the <pre> tag, write your current file's contents, then write the </pre> tag.

my $input = 'existing.txt'; my $output = 'new.txt'; open ( my $in, '<', $input ) or die "Cannot open file $input for readi +ng: $!\n"; open ( my $out, '>', $output ) or die "Cannot open file $output for wr +iting: $!\n"; print $out "<pre>\n"; while ( <$in> ) { print $out $_; } print $out "\n</pre>\n"; close $out or warn "Error writing $output: $!\n"; close $in;

Compare the above to either keeping track of which line you're on from the input or slurping the input file into memory and doing a regex substitution over the whole thing. I think the KISS principle applies here.