#! perl -slw use strict; # assuming the name of the compound file is supplied on the command line until( eof( ARGV ) ) { my @buffer = <>; ## Put the tag into a clean buffer push @buffer, scalar( <> ), scalar( <> ); ## Ditto & my( $filename ) = ( my $line = <> ) =~ m[(\S+)]; push @buffer, $line; open OUT, '>', $filename or die $!; print OUT for @buffer; print OUT until ( $_ = <> ) =~ m[]; close OUT; }