use strict; use warnings; my $fh; my $tag; while(){ print "\nNext iteration of while loop\n"; print "\$tag = $tag\n"; print "\$_ = $_"; chomp $_; $_ =~ s/[\cA-\cZ]//g; #$_ =~ s/^\s+$//g; #$_ =~ s/\n//g; #$_=~ s/[\r]//gs; if(/^{(.*)}/) { print "\tThis line is a tag line\n"; $tag = $1; print "\t\$tag has been set to $tag\n";; } else { print "\tThis line is not a tag line.\n"; if($tag eq 'FILE') { print "\t\t\$tag is equal to 'FILE', so this line gives a new filename\n"; my $filename = $_; # print "The filename is $filename\n"; open($fh, '>', "$filename.xml") or die "$filename: $!"; print $fh '',"\n"; print $fh "\n"; print $fh "$filename"; print "\t\tAny previously open output file has been closed\n"; print "\t\tand new output file $filename has been opened.\n"; print "\t\tand some initial text has been written to it.\n"; } elsif(defined($fh)) { print "\t\t\$tag is not equal to 'FILE' and we have an open output file.\n"; if( $_ ne ''){ #### Remove the blnak lines print "\t\t\tand the line is not blank\n"; print "\t\t\tso, write it to the output file\n"; print "\t\t\tas '<$tag>$_'\n"; chomp $_; print $fh "<$tag>$_\n"; } #print $fh ""; } } } close($fh); exit(0); __DATA__ ^B^B^B^B^B^B {FILE} sourcetag1 {NUMBER} 00000 {SOURCE} source1 {KEYWORD} {AUTHOR} author1 staff1 {HEADLINE} DISPOSABLE DECOR: THE CUTTING EDGE DULLS FAST\ STYLE AT A SPEED USUALLY ASSOCIATED WITH WARDROBE ITEMS. {FILE} sourcetag2 {NUMBER} 00002 {SOURCE} sourcenam2 {KEYWORD} {AUTHOR} author2 staff2