sub counttags { my $stag=0; my $etag=0; my $tag = quotemeta($_[0]); while ($_ =~ /<$tag]>/g) {$stag++} while ($_ =~ /<\/$tag>/g) {$etag++} if($stag != $etag) { print "Number of Start and End tags for element $_[0] does not match"; } } #### sub check_tags_balance { my @tagstack; while(/<(\/)?([^>]+)>/g) { unless($1) { # opening tag push @tagstack, $2; } else { # closing tag unless(@tagstack and $2 eq pop @tagstack) { print "Found an unbalanced closing tag $2\n"; return; } } } if(@tagstack) { print "Missing closing tags for @tagstack\n"; } else { print "Tags balanced.\n"; } } $_ = "I assume everything is ok"; check_tags_balance();