in reply to Read entire file then clear while flocking

You could take a look at File::Slurp's edit_file sub. This allows you to edit file contents. You could push the contents from $_ to another variable and then clear $_. I'm not sure how locks are done internally though.

use File::Slurp qw(edit_file); my $contents = ''; edit_file { $contents = $_; $_ = '';} 'temp/test.txt'; print $contents;