my $full_line = ''; while ( my $line = ) { chomp $line; next if substr( $line, 0, 2 ) eq '//'; $full_line .= " $line"; # space wanted? your call! next unless substr( $line, -1, 1 ) eq ';'; #do something with $full_line print "$full_line\n"; $full_line = ''; } #### $full_line .= $full_line eq '' ? $line : " $line"; #### some code; more code; some very long long long code;