+sub change() {
+ my ($mytrailer, $mybegin, $myend) =
+ ($Trailer, $MarkBegin, $MarkEnd);
+
+ my ($bm_found, $em_found); # begin/end mark found indicator
+
+ # Make the strings regexp-friendly by quoting non-word chars.
+ $mybegin =~ s/\W/\\$&/g;
+ $myend =~ s/\W/\\$&/g;
+ $mytrailer =~ s/\W/\\$&/g;
+
+ my (@filtered, $done, $skip);
+ $done = 0; # job done once
+ $skip = 0; # skip original block
+ foreach (@Lines) {
+ if (! $done and $skip > 0) {
+ if (/^$myend(?:$mytrailer)?$/o) {
+ ++ $done; # skipped all that was to skip
+ } else {
+ ++ $skip; # count lines being skipped
+ }
+ }
+ elsif (0 == $skip and $_ =~ /^$mybegin(?:$mytrailer)?$/o) {
+ push (@filtered, $StdinContent);
+ $skip = 1;
+ }
+ else {
+ push (@filtered, $_)
+ }
+ # for safety check:
+ $bm_found = 1 if (/^$mybegin(?:$mytrailer)?$/o);
+ $em_found = 1 if (/^$myend(?:$mytrailer)?$/o);
+ }
+ if ($bm_found and $em_found) {
+ -- $skip; # correct the counter
+ DEBUG and print STDERR "Replaced block of $skip lines\n";
+ @Lines = @filtered;
+ }
+ elsif ($bm_found and ! $em_found) {
+ # safety exit
+ die "$ProgramName: no end-mark after begin-mark!\n";
+ }
+ return $done;
+}
+