use Tie::File; my $count = do '/path/to/data.count'; tie my @lines, 'Tie::File', '/path/to/data.dat'; print $lines[rand $count];