I'm new to Mojolicious. I am sure this is probably a setup problem, but it's eaten up an entire day of my time. I'm trying to run this simple test code
#!/usr/bin/perl
use strict;
use warnings;
use Mojo::DOM;
use Mojo::UserAgent;
my $ua = Mojo::UserAgent->new();
$ua->get('https://stackoverflow.com/questions/26353298/find-links-containing-bold-text-using-wwwmechanize')->res->dom('a div')->ancestors('div.spacer')->each( sub { say $_->all_text } );
which I found at this location
find links containing bold text using WWW::Mechanize
and it is failing with
Can't locate object method "ancestors" via package "Mojo::Collection" at ./test4.pl line 10.
I've deinstalled and reinstalled a jillion times, tried different package installation options (cpan, cpanm, direct link, etc). No dice. I am mildly confused that the Mojo::Collection module does not appear to have an "ancestors" method in it (inherited or no), but I've seen several other examples like this which appear to use the same method in the same way. It's not just the "ancestors" module, either -- the problem seems to be affecting a few other methods as well.
I'm using perl 5.18.2 on Linux Mint with Mojolicious package 6.11.
Thanks for any help.
It's very difficult to debug a single long chained statement like that, and you're much better off splitting it into individual steps
Passing a parameter to the
dom
method is the same as callingfind
with that parameter on the DOM object.find
returns aMojo::Collection
, which makes sense as it's the set of nodes that match the CSS selector. Theancestors
method applies only to a single node, so you have to pick one out with something likefirst
orlast
, or process them all one at a time usingeach
Here's a rewrite of your code that produces what I assume is the expected result
output