I've finally got something that works. It's very sketchy and p6-newbish, but it does the job. Obviously there is a LOT more I need to learn, but I'm content that I've got a proof-of-concept done. The following code was written/tested on a FreeBSD system.
anonymonk above was right; :buf, Buf was the key here.
se v6;
use experimental :pack;
my $fn = 'in.txt';
my $outfile = 'out.txt';
# write a new file with win32 endings
my $fh = open $fn, :w;
$fh.print("ab\r\ndef\r\n");
$fh.close;
# re-open the file for reading
$fh = open $fn, :bin;
my $eol_found = False;
my Str $recsep = '';
# read one byte at a time, as we have no way to tell end of
# line from EOF, and we don't want to slurp the whole thing
while $fh.read(1) -> $buf {
my $hex = $buf.unpack("H*");
if $hex ~~ /(0d|0a)/ {
$eol_found = True;
$recsep = $recsep ~ $hex;
next;
}
if $eol_found {
if $hex !~~ /(0d|0a)/ {
last;
}
}
}
$fh.close;
my %recseps = (
'0d0a' => "\r\n",
'0d' => "\r",
'0a' => "\n",
);
my $nl = %recseps<<$recsep>>;
# open a new file for writing with our new recsep
$fh = open $outfile, :w;
$fh.print('a' ~ $nl);
$fh.close;
# re-read the outfile and see if our saved recsep
# was written
$fh = open $outfile, :bin;
my $buf = $fh.read(1000);
say $buf;
__END__
Buf[uint8]:0x<61 0d 0a>
Now I'll go back to reading the docs and practicing what I'm learning. I'm sure within a single day I can clean that code up a whole heck of a lot :)
After I'm more familiar with Perl6, I'll go back to attempting to convert my module.
Thanks for all the feedback here! |