Search code examples
htmlperldommojolicious

How to know if Perl Mojo::DOM::find returns or matches any DOM Element


Am currently parsing a series of webpages with Mojo::DOM and the only criterion for me to proceed down the web page is if there's an element found within.

I have my DOM object built like this:

my $urlMJ = Mojo::URL->new($entry->link);
my $tx = $ua->get($urlMJ);
my $base = $tx->req->url;
my $dom = $tx->res->dom;
my $divVideo = $dom->find('div#searchforme');

My question is, how do I know if $divVideo is empty?

I realise that from this question on google groups and grokbase answered by SRI (Riedel), if find doesn't match any element, it returns (if I get it correctly) the DOM object collection initiating the find and an empty DOM collection, which happens to be the result.

I thought of using an each to get to the empty DOM collection within, but won't the DOM returned contain the initial DOM structure?

I have tried using if (defined($divVideo)) , I also tried dumping with print Dumper($divVideo). All it returned was $VAR1 = bless( [], 'Mojo::Collection' );

I tried $dom->find('div#searchforme')->size , return values was 0 and even for those web pages that didn't fall into this category.

Can somebody please help me out?

Is my approach to this wrong?


Solution

  • if find doesn't match any element, it returns (if I get it correctly) the DOM object collection initiating the find and an empty DOM collection, which happens to be the result.

    You're misunderstanding find. It returns just a Mojo::Collection of Mojo::DOM objects that represent each matching element in the page. Nothing else. So if no matches are found, just an empty collection is returned

    This object has a size method, so you can say

    my $divColln = $dom->find('div#searchforme');
    
    if ( $divColln->size > 0 ) {
        ...
    }
    

    Alternatively you could use the each method to convert the collection into a list, and assign it to an array like this

    my @divColln = $dom->find('div#searchforme')->each;
    
    if ( @divColln ) {
        ...
    }
    

    Or if you are expecting to find just one such element (which it looks like you're doing here) then you can just pick the first item from the collection, like this

    my $divVideo = $dom->find('div#searchforme')->[0];
    
    if ( $divVideo ) {
        ...
    }