{ my $errors; open local(*STDERR), '>>', \$errors or die $!; # do stuff, can call external functions that warn or die if ($errors) { print error_template($errors); # however you do that exit 0; # or non-zero if we want to tell the server we failed } # maybe do more stuff - success branch }