script unexpectedly terminating

I now that this isnt the greatest code around. Im a network guy by trade not a programer .. but needed something to compare config files ...
Anyway ... intermittently, the program terminates.

Ive been looking at the code for a week trying to figure it out and Im stumped. Can anyone provide input. Thanks.

use English;
require Tk;
use Tk;
use Cwd;

#perl2exe_include "Tk/Text.pm"
#perl2exe_include "Tk/Scrollbar.pm"
#
$stim = localtime($^T);
$userName = $ENV{'USERNAME'};
my $main = MainWindow ->new;

$main->minsize(qw(275 120));
$main->configure(-title => 'MSD File Compare',
-background => 'black');
$main->geometry('+15+25');

#build main body
my $top = $main->Frame(-background => 'black',
)->pack(-side =>'top', -fill => 'x');
my $left1 = $top->Frame(-background=>'black',)->pack
(-side => 'left', -pady => 1, -padx => 9);
my $t1 = $left1->Label(-text => '', -foreground=> 'white',
-background=>'black')->pack();

#Get Files buttons/place status label
my $t1 = $left1->Button(-text => 'Get File One', -foreground=> 'white',
-background=>'blue', -command => [\&gfil1],)->pack();
my $t1 = $left1->Button(-text => 'Get File Two', -foreground=> 'white',
-background=>'blue', -command => [\&gfil2],)->pack();
my $left2 = $top->Frame(-background=>'black')->pack
(-side => 'left', -pady => 2, -padx =>15);
my $filloc = $left2->Label(-text=>'File Location and name',
-foreground=>'white', -background=>'black')->pack();
#
#File input fields
$filename = $left2->Entry(-textvariable => \$filnam1, -background=>'blue',
-foreground=> 'white',-width=>20, -borderwidth=>6, -relief=>'sunken')->pack();
$filename2 = $left2->Entry(-textvariable => \$filnam2,-background=>'blue',
-foreground=> 'white', -width=>20, -borderwidth=>6, -relief=>'sunken')->pack();
#
#build separator
my $lin=$main->Frame(-borderwidth=> 4, -relief=>'groove',
-background=>'blue')->pack(-side=>'top');
my $stat=$lin->Label (-width=>80, -height=>0,-foreground=>'white',
-background=>'blue', -anchor => 'n')->pack();
$stat->configure(-text=>"Status Display");

#set up frame to show results
my $fr1 = $main->Frame(-width => 250, -height => 280);
$fr1->pack;
my $text = $fr1->Scrolled("Text")->pack( );
$text->insert('end', "\t- V1.4\n");
$text->insert('end', "\t\n");
$text->insert('end', "\t$^O $stim $userName\n\n");
#
#Do check File1 to file2 button1
$main->Button(-text => 'Compare File One->File Two', -background=> 'blue',
-foreground=> 'white',
-command => sub{runit($filename, $filename2)},
)->pack(-side => 'left');
#Do check Finish-Start button3
$main->Button(-text => 'Compare File Two->File One', -background=> 'blue',
-foreground=> 'white',
-command => sub{runit3($filename, $filename2)}

         \)->pack\(-side => 'left'\);

#Do clear button
$main->Button(-text => 'Clear Output',-background=> 'blue',
-foreground=> 'white',
-command => \&clear_output
)->pack(-side => 'left');
#
#Exit Button
$main->Button(-text => 'Quit', -background=> 'blue',
-foreground=> 'white',
-command => [$main => 'destroy']
)->pack(-side => 'right');
#
MainLoop;

#
sub runit {
my ($file1, $file2) = @;
my $file1_val = $file1->get;
my $file2_val = $file2->get;
open (FILE1, "$file1_val") or die;
open (FILE2, "$file2_val") or die;
@xray=<FILE2>;
# take care of white space put in by router.
for ($i=0; $i<@xray; $i++) {
$xray[$i] =~ s/\s+//g;
$xray[$i] =~ s/\s+$//g;
push (@clean2, $xray[$i]);
}
$text->insert('end', "\nFile One to File Two Comparison.\n");
$text->insert('end', "Lines in the push config missing from the device config:\n");
$text->insert('end', "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * \n");
for $line (<FILE1>) {
$house=$line;
$line =~ s/\s+//g;
$line =~ s/\s+$//g;
if (!(grep $line eq $
, @clean2)) {
$text->insert('end', "$house");
}
}
close FILE1;
close FILE2;
}
#
sub runit3 {
my ($file1, $file2) = @;
my $file1_val = $file1->get;
my $file2_val = $file2->get;
open (FILE1, "$file1_val") or die;
open (FILE2, "$file2_val") or die;
@xray1=<FILE1>;
# take care of white space put in by router.
for ($i=0; $i<@xray1; $i++) {
$xray1[$i] =~ s/\s+//g;
$xray1[$i] =~ s/\s+$//g;
push (@clean, $xray1[$i]);
}
$text->insert('end', "\nFinish to Starting Comparison.\n");
$text->insert('end', "Lines in the Finish config not in the Starting config:\n");
$text->insert('end', "* * * * * * * * * * * * * * * * * * * * * * * * * * * * \n");
for $line (<FILE2>) {
$house2=$line;
$line =~ s/\s+//g;
$line =~ s/\s+$//g;
if (!(grep $line eq $
, @clean)) {
$text->insert('end', "$house2");
}
}
close FILE1;
close FILE2;
}
#
sub gfil1 {
$filnam1 = $main->getOpenFile(-title => "Select File One");
}
#
sub gfil2 {
$filnam2 = $main->getOpenFile(-title => "Select File Two");
}
#
sub clear_output {
$text->configure(-state => 'normal');
$text->delete('1.0', 'end');
}