Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

mojo
communitycreator
perl

What is the root method in Mojo::DOM?

Gutha Vamsi Krishna

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

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.

Syntax

$dom->root

Return values

This method will return the Mojo::DOM object of the root node.

Let’s look at an example.

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>

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>');

# Display the root element
say $dom->root;

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 scalar $dom.
  • Line 8: We print the Mojo::DOM object of the root element for the given HTML.

RELATED TAGS

mojo
communitycreator
perl

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring