my $texttosub = "log10(blackcat)"; #the value of "blackcat" can be found in a hash called "%cats" while ( $texttosub =~ s|([a-zA-Z][A-Za-z_0-9]+)|$cats{$1}|i ){ print ("\n", " The value of cat = ", eval ($texttosub) ); ..do something.. } sub log10{....} #### while ( $texttosub =~ s/(?!log10)|([a-zA-Z][A-Za-z_0-9]+)/$cats{$1}/i ){ ..do something.. }