AndrewMB has asked for the wisdom of the Perl Monks concerning the following question:

I have taken a working program in Linux which uses Tie::File, and transferred it to Windows (Vista) ActivPerl 5.8.8. The tied file (copied from Linux) uses \n as record separators so I added recsep => "\n" to the Tie. The program now seems to corrupt the file whenever a record is updated (the record length is changed). Subsequent references to records return text which does not correspond with record boundaries. Subsequent inserts are made into the middle of records. Is this a bug in Tie::File in Windows or have I forgotten something?
  • Comment on Tie::File appears not to work in Windows

Replies are listed 'Best First'.
Re: Tie::File appears not to work in Windows
by dasgar (Priest) on Aug 21, 2010 at 19:58 UTC

    If you're moving a file from Linux to Windows, there's a chance that you need to modify the formatting a bit. The two operating systems use different EOL line characters. On the Linux box, trying using the unix2dos shell command on the file, which would convert the EOL line characters. The only other thing that I can think of would be to use binmode.

    Some of the replies in Windows and UNIX end of line characters might help you out or better explain things.

    If you're still having problems, try posting some the code and data from the file and perhaps someone might be able to shed more light on the issue.

      Binmode solved it - many thanks! I guess because I'm using non-standard DOS terminators.
Re: Tie::File appears not to work in Windows
by Khen1950fx (Canon) on Aug 22, 2010 at 20:18 UTC
    File::ReadBackwards will do it automatically for you. Try this:
    #!/usr/bin/perl use strict; use warnings; use File::ReadBackwards; my $file = '/usr/lib/perl5/site_perl/5.8.8/File/ReadBackwards.pm'; tie *BW, 'File::ReadBackwards', $file, or die "can't read file $file: $!"; while ( <BW> ) { print; }
    Update: Here's a more complete version.
    It reverses the backwards file to forwards.
    #!/usr/bin/perl use strict; use warnings; use File::ReadBackwards; open(STDOUT, '>', 'tie.txt') or die "Couldn't open file: $!\n"; my $file1 = '/usr/lib/perl5/site_perl/5.8.8/File/ReadBackwards.pm'; tie *BW, 'File::ReadBackwards', $file1 or die "can't open file $file1: $!\n"; while ( <BW> ) { print; } my $file2 = '/home/tie.txt'; open(STDOUT, '>', 'tie2.txt') or die "Couldn't open file $file2: $!"; tie *BW, "File::ReadBackwards", $file2 or die "Couldn't open file $file2: $!"; while ( <BW> ) { print; }