#!/usr/bin/perl use strict; use warnings; my $dummy = ""; my $rollup = combine(""); my $oneline; while () { $rollup->($_); # once end is reached store it in a variable # reset variable-in-sub for next iteration if (/end;/) { # breaks if I remove $dummy and replace it with "" $oneline = $rollup->($dummy); $rollup = combine(""); print $oneline,$/; } } sub combine { my $concat = shift; return sub { chomp($_[0]); $concat .= $_[0]; return ($concat); } }