use strict; use warnings; my $stylesheet_file = "stylesheet.txt"; sub read_stylesheet { open(STYLESHEET, "<", $stylesheet_file) || die "Cannot read stylesheet_file\n"; my (%tag, $header); while () { if (/^HEADER\s+(\d+):/) { $header = $1; } elsif (/^nm\s+:\s+(\w+)/ { # Make a note of what header this tag # appeared in. $tag{$1} = $header; } } close(STYLESHEET); return \%tag; # Returns a reference to the hash } # ... (Main routine) my $input_file = "taggedfile.txt"; my ($output_file) = @ARGV; my $tag = read_stylesheet(); open(FILE, "<", $input_file) || die "Could not read $input_file\n"; open(OUT, ">", $output_file) || die "Could not write $output_file\n"; while () { # Perform substitutions s/^\{(\w+)\}/\{$1:$tag->{$1}\}/g; # Write line ($_) to OUT print OUT; } close(FILE); close(OUT); #### while (my $line = ) { # ... Use $line where you would normally use $_ }