見るに耐えないので書き直してみるテスト
use strict;
use warnings;
use IO::File;
use File::Spec;

my $datafile = './data.txt';
my $gif_dir = './image/';

my $fh = new IO::File $datafile, 'r+' or die q|Can't open datafile.|;
flock $fh, 2;
my $count = <$fh>;
truncate $fh, 0;
seek $fh, 0, 0;
print $fh ++$count;
undef $fh;

my @dat = map { File::Spec->catfile($gif_dir, "$_.gif") } split(//, $count);
# 以下CGIのため略

いや、問題の解決にはなってないけどね