Try this. Path::Tiny::spew writes to a temporary file and then atomic renames to the final name.
This should never allow an empty file.
#!/usr/bin/perl
use strict; # https://perlmonks.org/?node_id=11164885
use warnings;
use Path::Tiny;
#one.pl
my $file = '/dev/shm/tmpcount.txt';
for (1 .. 180) {
print "$_\n";
# &filewrite($file,$_);
path($file)->spew($_); # NOTE writes to temp file, then atomic renam
+es over
sleep 1;
}
#two.pl
my $file = '/dev/shm/tmpcount.txt';
my $c;
do {
if (-e $file) {
# $c = &fileread($file);
$c = path($file)->slurp;
die "C is empty" unless ($c);
print "count is $c \n";
} else { print "File not found\n"; }
} until ($c == 180);