Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks who are always smarter than me. I am parsing html pages using Mojo::Dom and have certain instances where the class I'm looking for is not on the page and therefore causing a can't call method "text" on an undefined value error. Here's my code:

$r7=$dom->at ('[class="MTLgp 654"]')->text ;

I tried pushing it to an array using the following but no luck, error-ing out:

$r7=$dom->at ('[class="MTLgp 654"]')->each(sub {push @columns 101, joi +n '|', map { $_->text };

Any help is greatly appreciated. I think I'm close with how I'm trying to fix it but off for sure

Replies are listed 'Best First'.
Re: Mojo Dom method "text" return undefined
by LanX (Saint) on Aug 31, 2021 at 01:41 UTC
    > can't call method "text" on an undefined value error

    So?

    my $c7 = $dom->at ('[class="MTLgp 654"]'); my $r7 = $c7->text if defined $c7;

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      Such a simple and elegant solution. I often over think it and over program. A sincere Thank You for your kindness And assistance from this novice programmer. Sincerely, Anthony

Re: Mojo Dom method "text" return undefined (block eval { } )
by Anonymous Monk on Aug 31, 2021 at 07:51 UTC
    choose your pick ;)
    my $summary = eval { $_->text }; my $title = "".eval { $_->text }; my $price = eval { $_->text } || 0;

    Excelsior! 🐕