in reply to Re: Special behavior for LF and CR in RegExs?
in thread Special behavior for LF and CR in RegExs?

Good catch on the missing 'g'!! You are right, that did work.

I have seen splitting on a \n work and also seen it not work. I'm using a compiled by myself perl 5.8.0 on Solaris 8 so perhaps there is a bug buried away in there?

Looks like davidrw's $/ suggestion also works. Given the above \n problem I think I will use that instead.

Thanks for all the help!!

-- Argel

  • Comment on Re^2: Special behavior for LF and CR in RegExs?

Replies are listed 'Best First'.
Re^3: Special behavior for LF and CR in RegExs?
by Aristotle (Chancellor) on Jan 05, 2006 at 01:39 UTC

    Well, $/ is the input record separator; generally, in strings and patterns, \n is magically mapped to that behind the scenes – even if it consists of multiple characters on the platform in question, such as CR/LF on DOS.

    Basically, using \n will always work so long as the data you’re processing comes from the same platform that you’re running on. If not, you’ll need to convert end-of-line markers. There’s no way to avoid this.

    So outside specific scenarios, you should use \n or $/ and let Perl handle the specifics. That will also yield the most portable scripts.

    Makeshifts last the longest.

      \n is not magically mapped to $/ "behind the scenes". There are so many misconceptions combined in that sentence that I'm at a loss at where to start.

      My reaction is strong because these many misconceptions are common, I've railed against them several times, and I respect you enough to be truely shocked to hear this from you.

      It's late and I'm very tired and yet also rather busy so I'll make the rude suggestion that you might want to super search for nodes by me regarding newline + $/ + \n + \r (not because I'm the only one who has anything useful to say on that subject, but because even using all of those terms, I suspect you'd otherwise get a lot to sort through while I'm sure I have several treatments of these all-too-common misconceptions under my name).

      Update: Struck out one search term to yield a more interesting search. Though the meat of it is mostly collected in Re^4: Line Feeds (rumor control).

      - tye        

        Ah, I was missing the last paragraph from the linked node. There is some magic mapping – I remember that much because I started learning Perl on Windows; but it happens elsewhere than what I thought – which I never found out first-hand because I left Windows behind at a time when I was still a Perl greenhorn.

        Thanks for the correction. (And yes, I know the issue is touchy. :-))

        I was going to ask a question to be sure whether I understood correctly, then I remembered binmode… and now it’s all coming back. Maybe I wasn’t so green back then; maybe it has simply been too long.

        At least, if my regained understanding is right, my advice is sound anyway: that in general, you want to use \n and not worry about it; and that if you have files from other platforms, you’ll have to convert anyway.

        Makeshifts last the longest.