Might be useful to factor out the match function, like

    sub dns_match {
        my( $check_dns, $san_dns ) = @_;

        my $re        = "";
        my $starmatch = "*.";

          # turn a leading "*." into a regex
        if( $san_dns =~ /^\*\./ ) {
            $san_dns =~ s/\*//;
            $re = "[^.]+";
        }
 
          # quotemeta the rest and match anchored
        if( $check_dns =~ /^$re\Q$san_dns\E$/ ) {
            return 1;
        }
        return 0;
    }