0

I am trying to replace newline characters in the DB with <br/> tags and have the following mySQL query string written in Perl.

my $queryString = "Select REPLACE(r.form_text,'\n','<br /> '), ... FROM myTable r;"

For some reason, \n is treated as a newline from within perl and does not seem to replace it with <br/>. Here is what I got when I tried to print $queryString

REPLACE(r.form_text,'

','< br /> ')

I tried to use \\n and \\\n and it didn't work either. I apologize if this is a repeated question. Please let me know if I'm missing something here.

eggyal
  • 122,705
  • 18
  • 212
  • 237

1 Answers1

0

Of course you get a newline. When you use double quotes to quote a string anything inside it is interpolated, and \n becomes a literal newline. If you want a literal \n, you need to prevent interpolation. Usually, you would use single quotes, but that is impractical since you have those in the string already. So instead, use the q() feature.

my $queryString = 
    q|Select REPLACE(r.form_text,'\n','<br /> '), ... FROM myTable r;|;

Note that you can use a wide variety of delimiters for q(), in this case, I used pipe |. If you try and print this string, it should have a literal \n. Documentation here.

TLP
  • 66,756
  • 10
  • 92
  • 149
  • On the flip side, the query should still have worked as intended even with a literal newline... – eggyal May 01 '12 at 15:50
  • Thanks a lot @TLP. That's something I learnt new today. But it prints as Select REPLACE(r.form_text,'\\n','
    ') FROM myTable r; Not sure what I'm missing here.
    – script3man May 01 '12 at 16:26
  • Me neither.. when I try it, I get `\n`. – TLP May 01 '12 at 16:40