What is the Mojo::DOM tag?

Overview

The tag method is used to find the tags of the given HTML. This method is provided by the Mojo::DOM module, which is an HTML/XML DOM parser with CSS selectors.

Syntax

$dom->tag

Return values

This will return the tag for the given Mojo::DOM object.

Let’s look at an example.

Example

The given HTML is as follows:

<div>
   Inside div 
   <p id="a">Inside first paragraph </p>
   <p id="b">Inside second paragraph</p>
</div>

If we try to find out the tags of the child elements of the div element using the tag method, we’ll get the following output for the given HTML.

p
p

Code example

use 5.010;
use Mojo::DOM;
# Parse the html
my $dom = Mojo::DOM->new('<div>Inside div <p id="a">Inside first paragraph </p><p id="b">Inside second paragraph</p></div>');
# Find tags
say $dom->at('div')->children->map('tag')->join("\n");

Code explanation

In the above code snippet, we see the following:

  • Line 2: We import the Mojo::DOM module.
  • Line 5: We parse the HTML and store it in the scalar $dom.
  • Line 8: We’ll get the tag names of the child elements of the given element div using the tag method, and print them with a new line.

Free Resources