Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

find
mojo
communitycreator

What is the find() 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 find() method will find all the descendant elements for the given element. This method is provided by the Mojo::DOM module, which is an HTML/XML DOM parser with CSS selectors.

Syntax

$dom->find('html element')

Return value

This method will return all the descendant elements as Mojo::Collection where each element is a Mojo::DOM object.

Let’s look at an example of this:

Code

use 5.010;
use Mojo::DOM;
# Parse the html
my $dom = Mojo::DOM->new('<div>Inside div <p id="a">Inside paragraph </p><h1>Inside heading 1</h1></div>');
# find descendants for div
say $dom->find('p')->map('tag')->join("\n");
Calling find() and returning descendants of the elements p

Explanation

In the code snippet above:

  • Line 2: We import the Mojo::DOM module.
  • Line 5: We parse the HTML and store it in scalar $dom.
  • Line 8: We get all descendants of the element p for the given HTML using the find method and print all tag names present in the returned Mojo::Collection.

RELATED TAGS

find
mojo
communitycreator

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