Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Tie::File Newline Issue

by omegaweaponZ (Beadle)
on Jan 30, 2014 at 15:36 UTC ( #1072692=perlquestion: print w/replies, xml ) Need Help??
omegaweaponZ has asked for the wisdom of the Perl Monks concerning the following question:

I have a question regarding how newlines are added at the end of each line record of an array when you use Tie::File for a local file. After modification, while when curling or opening the file in an advance text editor like wrangler, I see the newline after record insert without an issue. But on a no-format text editor like Notepad, I don't see the newline and two lines are combined. Any idea why this is the case? I specify the newline in the new record so I'm not sure why its not picking it up....
This is as basic as the code gets. Use Tie::File on a file, on a particular line insert something, and at the end use \n (which should be unnecessary anyway). But it would seem that line 3 and line 4 become combined on a no-format text editor like notepad, but appears correct on curl or an advance text editor. Any idea why?
#$file contents below.... #Line1 - Blah #Line2 - Blah #Line3 - Blah #Line4 - Blah #Line5 - Blah tie @file_lines, 'Tie::File', $file or die; $file_lines[3] = "This is my new record on line 3,\n"; untie @file_lines or die "$!"; #$file contents now has (on no format text editors)... #Line1 - Blah #Line2 - Blah #Line3 - This is my new record on line3,Blah #Line4 - Blah #Line5 -

Replies are listed 'Best First'.
Re: Tie::File Newline Issue
by Athanasius (Chancellor) on Jan 30, 2014 at 16:19 UTC

    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,

      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.

      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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1072692]
Approved by boftx
Front-paged by toolic
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2018-06-19 22:21 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (115 votes). Check out past polls.