P.Henderson
P.Henderson

Reputation: 1091

Perl Mojo::DOM modifying value of attribute

I would like to modify the value of an attribute, if another attribute contains a certain string.

my $dom = Mojo::DOM->new('<link href="http://google.com/feed/" rel="alternate">');
$dom->at('link[href*="google"]')->replace_content('http://www.yahoo.com/feed/');
print $dom;

So if, tag "<LINK" contains the word "google" in the HREF attribute, it should change the HREF attribute to yahoo. But instead the output is:

<link href="http://google.com/feed/" rel="alternate">http://www.yahoo.com/feed/</link>

But I want it to be:

<link href="http://www.yahoo.com/feed/" rel="alternate">

I realize the above is not meant to work (replace_content() modifies the content, not the attribute), but it is just to explain what I want.

Thank you

Upvotes: 1

Views: 897

Answers (1)

doubleDown
doubleDown

Reputation: 8408

Use the attr method instead, i.e.

$dom->at('link[href*="google"]')->attr(href => 'http://www.yahoo.com/feed/');

it can set attributes to specified value.

p/s: The attrs method has been deprecated (and eventually removed) in favor of attr.

Upvotes: 2

Related Questions