in reply to Re: Line separators when passing multi-line fields to a database ("\n")
in thread Line separators when passing multi-line fields to a database

Yes, I did believe that about Perl, and it looks from what you say as though I had got confused somewhere, as I don't remember reading perlport. But it's not there that I'm having a problem. It's with the text from the database that may contain either one or two characters that I need to convert to \n (that's the easy bit) and then pass unescaped back to the database. That's the bit where I'm stuck right now. But thank you for the correction, as I'm pretty sure I would otherwise get bitten some time in the future.

Regards,

John Davies

  • Comment on Re^2: Line separators when passing multi-line fields to a database ("\n")

Replies are listed 'Best First'.
Re^3: Line separators when passing multi-line fields to a database ("\n")
by tye (Sage) on Mar 21, 2011 at 19:15 UTC

    '\n' eq '\\n'; they are both two characters, a backslash (\) followed by the letter en (n). When you use placeholders, passing '\n' in as a value acts the same as putting a literal '\\n' into the SQL (at least for SQLs where '\\' is how you get a single backslash character).

    "\n" is a newline. When you use placeholders, passing "\n" in as a value acts the same as either putting '\n' (on some databases) or '
    '
    (a literal newline between single quotes) into the SQL.

    It sounds like all you need is s/\r\n/\n/g (unless you need to run your script on old Macs, in which case you want something ugly like:

    my( $cr, $lf )= ( "\r", "\n" ); ( $cr, $lf )= ( "\n", "\r" ) if "\r" eq "\x0a"; # On an old Mac ... s/$cr$lf/$lf/g;

    or you can just ignore the currently even rarer possibility of non-ASCII Perl and do s/\x0d\x0a/\x0a/g, but I refuse to go back to the "hard code character encodings" school of programming even if the current thinking is that Unicode will be the one ring to rule them all forever... eventually).

    - tye        

      OK, I have what I need. I had several problems that I will confess for the record. While I don't think it compounded the problem, my misunderstanding of Perl's \n that tye corrected would, I'm sure, have slowed me down eventually. Second, I hadn't seen Perl's \r before, didn't recognise it at first and, for no good reason, started thinking that \n was 0D and \r was 0A. Third, I was flailing at demons and trying to sort out the \n and \r combinations in text, instead of working with hex or decimal character codes. This led to a fourth problem, namely that I was trying to resolve the difference between \n as a two character string and 0A which gets db->quoted to \n. Fifth, I was unsure about the workings of \n in regexes and tried to do it a way I understood instead of a way that worked.

      My final solution is breathtakingly simple. s/\r\n/\n/g does everything I need. Even with the help I got here, it took the thick end of a day to unconfuse myself. I dread to think how long it would have taken on my own.

      Plusses and kisses to all who helped! :-)

      Regards,

      John Davies

      Update - much, much later. I continued to flail at demons even after posting the above. Strange things were happening that I could not explain and my database was not being updated. The regex I gave above, s/\r\n/\n/g, works provided there is only one line break, the case I was testing when I wrote my original query. What I really needed was s/\r\n/\n/gms to cover the case of three or more lines.