my %config = ( to => { regex => '^To:\s', sub => \&to }, from => { regex => '^From:\s', sub => \&from }, subject => { regex => '^Subject:\s', sub => \&subject }, ); my %cnf; # Load user data open DATA,'sss.txt' or die $!; foreach () { chomp; foreach my $c (keys %config) { if (/$config{$c}{regex}/) { $cnf{$c} = $_; last; } } }