in reply to running same bit of code twice

Untested, and I'm not sure about some of the variables.

use File::Temp 'tempfile'; use File::Copy; use Mail::Address; my $datafile = 'some file'; open( my $fh, $datafile ) or die "Cannot read $datafile: $!\n"; my ($out, $newfile ) = tempfile(); my %addresses = map { my ($user, $domain) = Mail::Address->parse( $_ ); $domain => $user; } 'bob@domain1.com', 'bob@domain2.com'; my $domains = qr( '(' . join('|', map { quotemeta( $_ ) } keys %addresses) . ')' ); while (<$fh>) { print $out $_; print $out "$addresses{ $1 }\@$1\t$addresses{ $1 }\n" if /$domains/; } close( $out ); close( $fh ); move( $newfile, $datafile );