use strict; use warnings; use HTML::TokeParser; my $p = HTML::TokeParser->new("file.html") # your source file ||die "Cant open: $!"; my $depth=0; while (my $token = $p->get_token) { if (lc(${$token}[1]) eq "table"){ $depth++ if (${$token}[0] eq "S"); $depth-- if (${$token}[0] eq "E"); print "$depth\n"; } }