while ( $sth->fetch() ) { chomp $x; last if ( $x =~ /^foo/ ); print "$x\n"; }