in reply to Re3: otherwise condition
in thread otherwise condition

Or just drop the chop altogether and use
$chain=substr($line,length($line)-2,1);
instead of
$chain=substr($line,length($line)-1,1);