in reply to bullet proof SLURP file

Do you have control of the process that writes to the file? If so, assure that it implements file locking, and cause it to also create a checksum that is saved in another file alongside the primary file. After writing, verify the checksum matches what got written.

Then the reader can obtain a lock, read the checksum, read the target file, compare, and derive a strong assurance that the entire file was read.

Otherwise, check file size before the read and after the read. Check the mtime before the read and after. If it changed, that's bad. Use the bytes pragma and get the length of the scalar holding the input. Compare that length to the file size detected before the read. And check for eof after the read.


Dave

Replies are listed 'Best First'.
Re^2: bullet proof SLURP file
by karlgoethebier (Abbot) on Jan 10, 2018 at 20:19 UTC

    Perhaps with something like this?:

    #!/usr/bin/env perl use strict; use warnings; use Path::Tiny; use feature qw(say); use open ':encoding(UTF-8)'; my $file = q(data.txt); path($file)->spew_utf8(q(Lorem ipsum kizuaheli)); my $digest = qq($file\.digest); path($digest)->spew_utf8( path($file)->digest ); # path ($digest)->append_utf8("nose"); if ( path($file)->digest ne path($digest)->slurp_utf8 ) { say q(Something went wrong!); } else { my $fh = path($file)->filehandle( { locked => 1 }, "<" ); say <$fh>; } __END__

    Not really tested. Just an idea.

    Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help