my $codeblock = "Important and unimportant stuff"; eval { doImportantStuff; #if anything goes wrong, it goes `die "invalid important stuff\n";` #This should be logged doUnimportantStuff; # `die "invalid unimportant stuff\n";` # Should not be logged. moreImportantStuff; #`die "more invalid important stuff\n";` # Should be logged }; log_error("$@", $codeblock) if $@; sub log_error { return unless DEBUG; my $error = shift @_; my $place = shift @_; my @logthese = ( "invalid important stuff\n", "more invalid important stuff\n", ); for my $check (@logthese) { if ($error eq "$check") { write_to_log("Error '$error' at $place"); last; # found! No need to continue searching } } }