Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

mojo
communitycreator
perl

What is the at method of 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 at method is used to find the first descendant node 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->at('html element')

Returns

This method will return the Mojo::DOM object.

Let’s take a look at an example.

Example

A sample HTML code is given below.

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

If we find the first descendant of the element p using the at method, we will get the following output:

<p id="a">Inside first paragraph </p>

Code

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>');
# Usage of at
say $dom->at('p')

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 find the first descendant of the element p and print its Mojo::DOM object.

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