The root
method is used to get the Mojo::DOM
object of the root element. This method is provided by the Mojo::DOM
module, which is an HTML/XML DOM parser with CSS selectors.
This method is useful when we want to get the root element and perform operations on its Mojo::DOM
object.
$dom->root
This method will return the Mojo::DOM
object of the root node.
Let’s look at an example.
Consider the given HTML:
<div>
Inside div
<p id="a">Inside first paragraph </p>
<p id="b">Inside second paragraph</p>
</div>
If we try to get Mojo::DOM
object of root node using the root
method, we will get the following output:
<div>Inside div <p id="a">Inside first paragraph </p><p id="b">Inside second paragraph</p></div>
use 5.010;use Mojo::DOM;# Parse the htmlmy $dom = Mojo::DOM->new('<div>Inside div <p id="a">Inside first paragraph </p><p id="b">Inside second paragraph</p></div>');# Display the root elementsay $dom->root;
In the above code snippet, we see the following:
Mojo::DOM
module.$dom
.Mojo::DOM
object of the root element for the given HTML.