#!/usr/bin/perl -w use strict; local $/=undef; my @objects; # check for basic syntax if ($#ARGV < 0) { die "Usage: program.pl file.text\n"; } my $rgxpar = qr{(^begin\n(\d+)\n.*?^end$)}mos; open (FILNAM, '<', $ARGV[0]) or die "Can't open $ARGV[0] for reading.\n"; my $allfile = ; close FILNAM or die "Can't close $ARGV[0] for reading.\n"; while ($allfile =~ /$rgxpar/g) { my $objectref = 'ref' . $2; if ($1 =~ /bar/ ) { push (@objects, $objectref); } } for (@objects) { $allfile =~ s/^ *(foo)? +$_\n//mn; } open ( OUTFIL, '>', "$ARGV[0].removed") or die "Can't open $ARGV[0].removed for writing.\n"; print OUTFIL $allfile; close OUTFIL or die "Can't close $ARGV[0].removed for writing.\n";