in reply to Tie::File Newline Issue

From the documentation for Tie::File:

What is a 'record'? By default, the meaning is the same as for the <...> operator: It's a string terminated by $/, which is probably "\n". (Minor exception: on DOS and Win32 systems, a 'record' is a string terminated by "\r\n".)

For whatever reason, on Windows the line:

$file_lines[3] = "This is my new record on line 3,\n";

inserts \n only, so the “records” become confused. Change to:

$file_lines[3] = "This is my new record on line 3,\r\n";

and it works as expected. I don’t know if this is a bug, but it looks like one.

Note: $file_lines[3] accesses the fourth line, as line counting starts at zero.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: Tie::File Newline Issue
by Lotus1 (Vicar) on Jan 30, 2014 at 20:29 UTC

    From the end of the recsep section of Tie::File you quoted

    Inserting records that contain the record separator string is not supported by this module. It will probably produce a reasonable result, but what this result will be may change in a future version. Use 'splice' to insert records or to replace one record with several.

    I tried using Tie::File to add "\n" at the end of a line on Windows 7 and it did not concatenate two lines as the OP claims. In notepad every record was on a separate line. In notepad++ there was an extra blank line inserted. Since "\n" is not the record separator for Win7 it seems resonable the module would put it in like other text.

    The point of Tie::File is the convenience of treating a file like an array so the OP seems to miss this by fiddling with record separators.

Re^2: Tie::File Newline Issue
by omegaweaponZ (Beadle) on Jan 30, 2014 at 16:34 UTC
    Ugh...Windows. Always trying to be different. Yes, including both \r AND \n ensures the newline appears in their notepad view and any other view. Appreciate the insight into the way Win32 defines new records as this