my $bytes_written = 0; my $bytes_wanted = 2**30; # or whatever my $data = "Something...\n"; # set output content here... while (1) { # $data = ...; # ... or here print $fh $data; $bytes_written += length( $data ); last if $bytes_written >= $bytes_wanted; }