Linux:Program exit with displaying a print feature

hi All
I have a scritp running which connects to a local host and then gets a value from a field and then ftp that value to antoher server. It is running fine, and from crontab it gives the output to a file, the problem is sometime it doesnt run but if i check the output file it does not show one of the print statement which is in with in the exit loop,
Not sure where it end the loop and exit out of the script, anyhelp would be very appreciated

use IPC::Open2 ;
$wmscmd = "/opt/WMS/rel/bin/wms_tl1";
my $pid = open2(\*WMSRDR, \*WMSWTR, "$wmscmd") ;
print "Trying to Connect...\n";
while (my $junk = <WMSRDR>)
        {
        chomp $junk ;
        print "$junk\n";
        `sleep 1`;
        last if $junk =~ /Connected to: localhost,2361/ ;
        }
print WMSWTR "swuser\n";
print WMSWTR "wmspasswd\n";
sleep(3);
print WMSWTR "RTRV-switch::WMS:C1::avgNoOfParties;\n";
while (my $result = <WMSRDR>)
        {
push(@LIST,$result);
print "RES:$result\n";
        if ($result =~ /avgNoOfParties=/)
                {
                $temp = substr($result,18);
                }
        last if $result =~ /avgNoOfParties=/ ;
        }
chomp($temp);
$acp = substr($temp,0,-2);
 

print "AVG: $acp";  -----  the output file doesnt display this print even if it was not a succeful run
 
sleep(1);
print WMSWTR "quit\n\n";
sleep(1);
close(WMSWTR);
close(WMSRDR);

Output file: (in case of not succesful run)
Trying to Connect...
Connected to: localhost,2361