while( $run ) { my $newline = $objPort->lookfor; if ( $newline ne '' ) { print $newline; $run = 0 if $newline =~ /reason to stop/; } }