http://qs321.pair.com?node_id=876158


in reply to why isn't split() working here?

7stud:

If you set $whole_file = "aaa\nbbb\nccc\nxxx\nyyy\xzzz"; in your first example, you'll see that it works there, too. I'd guess that you're on a windows box and your DATA section actually looks like: "aaa\r\nbbb\r\nccc\r\nxxx\r\nyyy\r\nzzz\r\n". If that's true, then changing your split expression to /xxx\r?\n/ or /xxx[\r\n]+/ might save the day for you.

...roboticus

When your only tool is a hammer, all problems look like your thumb.

Replies are listed 'Best First'.
Re^2: why isn't split() working here?
by JavaFan (Canon) on Dec 09, 2010 at 09:59 UTC
    Or if you are running 5.10 or newer:
    split /xxx\R/
Re^2: why isn't split() working here?
by 7stud (Deacon) on Dec 09, 2010 at 03:42 UTC
    Thanks. I was using codepad.org to run some code, and per your suggestion if I switch \n to \r\n, then my code works. I was using codepad.org to run some code using a windows computer, but I still don't quite understand why perl doesn't seemlessly translate \r\n to a \n. When I hit return on a windows computer, I should be entering \r\n--not just \r.
      Perl does seamlessly convert \r\n to \n on Windows unless you binmode. It would be nice if there was a layer that handled common line endings seamlessly, but noone's gotten around to doing it. It's not that hard, even.
      I still don't quite understand why perl doesn't seemlessly translate \r\n to a \n.

      Presumably, the perl that codepad.org is running is a Unix-built perl, which means it does not add a :crlf PerlIO layer to file handles by default (as is done on Windows). The :crlf layer is responsible for transparent \r\n <—> \n linefeed translations (see PerlIO).

      Adding binmode DATA, ":crlf" before slurping in the data should emulate Windows perl behavior on codepad.org.