What is the at method of Mojo::DOM?

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.

Free Resources