open $srcFile, '<', 'source.txt' or die $! my $metaText = ""; while( my $line = <$srcFile> ) { if( $line =~ m/(.*)<\/text>/ ) { # Replace the old meta text with the new one. $metaText = $1; } else { # Normal line. The old meta text applies. my @words = split /\s+/ $line; foreach my $word (@words) { store_in_index($metaText, $word); } } } close $srcFile;