while(){ # get the names of the fields on this line my @variables = $_ =~ m/<\?--(\S+?)-->/g; for my $variable ( @variables ){ # replace the tag with the value of the variable # if there is one. s/<\?--$variable-->/$$variable/g if $$variable; } }