in reply to using HTML::TreeBuilder causes "premature end" error

Try adding use CGI::Carp qw[fatalsToBrowser]; above the commented line to see if it adds any additional knowledge to your problem.