You should read
perlintro, and start over, that code is awful.
#!/usr/bin/perl --
use strict;
use warnings;
use CGI();
use CGI::Carp qw(fatalsToBrowser);
my $html = <<'__HTML__';
all your html , exactly as you want it
__HTML__
my $freaky_number=int(rand(9999)) + 1;
my $check_number=rand(9999);
my $k_weather = "../weather_/".$freaky_number.".txt";
if( open DDATA, ">$k_weather" ){
print DDATA $html;
close DDATA;
} else {
warn "Can't open output file $k_weather: $!";
}
my $q = new CGI;
print $q->header(), $html;