use HTML::TagReader;
my $filename = 'test2.html'; # Here instead of using this file I want to do same thing using HTML as a string in some variable say $html_string = 'content of test2.html'
my $p=new HTML::TagReader "$filename";
open(my $fh, '<', $filename) or die "Could not open file '$filename' $!";
my %line_chars;
my $line_number = 1;
while (my $row = <$fh>) {
if ($line_number > 1) {
$line_chars{$line_number} = $line_chars{$line_number - 1} + length($row);
}
else {
$line_chars{$line_number} = length($row);
}
$line_number++;
}
my @atags;
my %atagrange;
while(my ($tagOrText,$tagtype,$linenumber,$column)=$p->getbytoken($showerr)) {
my $position;
my $a_start_tag_pos;
if ($linenumber > 1) {
$position = $line_chars{$linenumber - 1} + $column;
}#print "\ntagOrText:" . $tagOrText . "\ntagtype : " . $tagtype . "\nline number :" . $linenumber . "\ncolumn : " . $column . "\nposition : " . $position . "\n";
if ($tagtype eq "a" or $tagtype eq '/a') {
if ($tagtype eq "a") {
push(@atags, $position);
}
else {
$a_start_tag_pos = pop(@atags);
$atagrange{$a_start_tag_pos} = $position;
}
}
}