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.

In reply to Re: Adding <pre> at beginning </pre> at file end by mr_mischief
in thread Adding <pre> at beginning </pre> at file end by vinoth.ree

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.