On 5/10/06, D. Bolliger <[EMAIL PROTECTED]> wrote:
Ankur Gupta am Mittwoch, 10. Mai 2006 11.23:
> Hi,
>
> Using Tie::File with recsep = "\n\n" adds two extra lines to the
> original file when I iterate through the entire file. If I just change
> one record then its fine.
>
> Below is the program
>
> use strict;
> use warnings;
>
> use Tie::File;
>
> my @array;
> tie @array, 'Tie::File', 'file', recsep => "\n\n" or die $!;
>
> foreach my $rec ( @array ){
> print $rec;
> }
>
> untie @array;
>
> $-> diff file file.orig
> 8464,8465d8463
> <
> <
>
> Right now, I am tieing the file again without recsep and popping two
> records.
>
> tie @array, 'Tie::File', 'file' or die $!;
> pop(@array);
> pop(@array);
> untie @array;
>
> This fixes the problem but is there a better way to get around this?
>
> perl version is 5.8.0 and latest version of Tie::File (0.97).
Hi Ankur Gupta
Your file stays unchanged only if it ends with recsep, "\n\n" in your case.
(So one workaround instead of the two pops would be to make sure the input
file contains this recsep at the end)
Thanks for letting me know. I didn't know this until you told me and I
tried it out. Now I can not use the pop*2 solution as it would have
deleted unnecessary lines if the record was ending with the recsep. I
can keep the file in the correct state by adding recsep at the end,
but the problem is that the file can be edited by anyone in my team
using the script or by hand. So I added the following before untie
pop(@array) if $array[-1] eq '';
So now this will not create any extra records even if the last record
does not end with recsep. It may add a new line which I am ok with.
This behaviour is described in perldoc Tie::File, but not enough prominent
imo (and done by _fixrecs()).
I should have read the document clearly and completely. It does say abt it.
[...]
There is no way to create a file whose trailing record separator
string is missing
[...]
I think not all people expect the tied file to be altered when no write
access is done. In Ankur Gupta's example, records are even added with every
(un)tie.
That is true. If I am not altering any record, I want it to be unchanged.
I'm not sure if this is a bug. Should the file stay unaltered if it's never
accessed for writing? Should the last record even be treated specially?
Can't it make an exception for the last record?
If it can't be fixed, can it be added to the CAVEATS section.
And would be great if a workaround is suggested.
Anyway, I like this module and will be using it as it provides a nice
abstraction in dealing with files.
--Ankur
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>