i read the Q a little different than my other, fellow monks did - so, i'm gonna answer it this way.(Can't hurt. Someone may need the info, regardless.)

the way i take it, is that you're reading from say - a flatfile (.txt) database - trying to swap out the perl in favour of html, so it can be read by a browser or otherwise converted to html. In other words, you're not exchanging "newlines" but the perl command-like (meta-)characters that represent newlines (\n).

if i'm not mistaken...
perl considers the / / in a regex the same as it does double-quotes " " - which means, they're interpolated (carry their full worth - variables are substituted for their values, etc). So, exchanging \n would be exchanging an actual newline - rather than newline characters.

(i know, it's confusing.)

anyways...
if i were exchanging "\n" for "<br>\n", i'd make sure, that the characters were all turned off ("just characters") and not something with more meaning.

like...

my $comments =~ s/\\n/\<\.br\>\\n/;

that extra slash before the special (meta) character tells perl to "turn the special meaning off" (or rather "escape the character" in perl lingo.)

or, i believe, single-quotes, which don't interpolate - might work as well -

my $comments =~ s/'\n'/'\n<.br>\n'/;

(i almost never use that method, so can't be 100% sure, how well it works.)

in short... if you have a text file, that's got actual tabs, line-breaks, etc in them - try regexes joost and tachyon are suggesting. If you have in the file the actual characters...

hi\t my name is Count Dracula\n

use this.

update...just for the record, i don't think the angle brackets <> need to be escaped. (oops)


In reply to Re: Substituting Newline Characters by wolfi
in thread Substituting Newline Characters by bkiahg

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.