The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

HTML::TagUtil - Perl Utility for HTML

SYNOPSIS

  use HTML::TagUtil;
  $_ = "<i>Now!</i>";
  
  my $tagger = HTML::TagUtil->new();
  print "Tagged!"       if ($tagger->tagged());
  print "Open Tagged!"  if ($tagger->opentagged());
  print "Close Tagged!" if ($tagger->closetagged());

DESCRIPTION

HTML::TagUtil is a perl module providing a Object-Oriented interface to getting information about HTML/SGML/XML tags and their attributes and content.

METHODS

new

new is the constructor for HTML::TagUtil. it can be called like this: my $tagger = new HTML::TagUtil (); my $tagger = HTML::TagUtil->new();

also, you can supply an optional argument as the string to use if none is given to one of the methods. if you do not supply it here, it defaults to the default variable ($_) here and everywhere else.

$tagger->tagged

tagged checks to see if a string has both an end tag and a start tag in it. if it does, it returns true, if not, it returns false. a few examples would be:

 $_ = "<html>html stuff</html>";
 print "Tagged" if ($tagger->tagged); #prints "Tagged"
 $_ = "<html>html stuff";
 print "Tagged" if ($tagger->tagged); #prints nothing.
 $_ = "html stuff</html>";
 print "Tagged" if ($tagger->tagged); #prints nothing.
 $_ = "<html blah="blah_blah">html stuff</html>";
 print "Tagged" if ($tagger->tagged); #prints "Tagged"

tagged can handle attributes and empty elements.

$tagger->opentagged

opentagged checks to see if a string has one or more start tags in it, ignoring whether it has an end tag in it or not. if it does have a start tag, it returns true. otherwise, it returns false. some examples are:

 $_ = "<html>stuff";
 print "Open Tagged" if ($tagger->opentagged); #prints "Open Tagged"
 $_ = "<html>stuff</html>";
 print "Open Tagged" if ($tagger->opentagged); #prints "Open Tagged"
 $_ = "stuff</html>";
 print "Open Tagged" if ($tagger->openedtagged); prints nothing
 $_ = "<html some="cool" attributes="yes">stuff";
 print "Open Tagged" if ($tagger->opentagged); #prints "Open Tagged"
 

opentagged can handle attributes as well as empty elements.

$tagger->closetagged

closetagged checks to see if a string has one or more end tags in it, ignoring whether it has a start tag or not. if it does have an end tag, it returns true, otherwise, it returns false. some examples are:

 $_ = "stuff</html>";
 print "Close Tagged" if ($tagger->closetagged); #prints "Closed Tagged" 
 $_ = "<html>stuff</html>";
 print "Close Tagged" if ($tagger->closetagged); #prints "Closed Tagged"
 $_ = "<html>stuff";
 print "Closed Tagged" if ($tagger->closetagged); #prints nothing.
 $_ = "stuff</html stuff="cool">";
 print "Closed Tagged" if ($tagger->closetagged); #prints nothing.

closedtagged can not handle attributes or empty elements. because end tags can't have attributes or be empty.

$tagger->tagpos

tagpos returns the position that a certain tag is at in a string, 0 meaning that it is not there, and 1 meaning the first position in the string and so on. It will add the < and the > on to the tag you specify if you do not. some examples are:

 $_ = "<html>stuff</html>"; 
 my $pos = $tagger->tagpos ($_, '<html>', 0);
 print $pos; #prints "1"
 $_ = "<html>stuff</html>";
 my $pos = $tagger->tagpos ($_, 'html', 0);
 print $pos; #prints "1" because the < and > get added on to the 'html'.
 $_ = "stuff<html>";
 my $pos = $tagger->tagpos ($_, '<html>', 0);
 print $pos; #prints "6" because counting starts from one for this.
 $_ = "stuff<html>";
 my $pos = $tagger->tagpos ($_, 'html', 0);
 print $pos; #prints "6" again because counting starts from one for this.
 
 tagpos can handle anything that is surrounded by < and >.

EXPORT

 tagged
 opentagged
 closetagged

BUGS

none known.

SEE ALSO

HTML::Parser HTML::Tagset

HTML::TagUtil's website is http://www.x-tac.net/html-util.htm/

AUTHOR

<nightcat>, <nightcat@crocker.com>

COPYRIGHT AND LICENSE

Copyright 2005 by <nightcat>

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.