Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: HTML::Element accessing "internal attributes" the proper way

by jcb (Parson)
on Dec 25, 2020 at 03:27 UTC ( [id://11125733]=note: print w/replies, xml ) Need Help??


in reply to HTML::Element accessing "internal attributes" the proper way

A quick look at the HTML::Element documentation suggests using the content_list method, as in my $date = ($d->content_list)[1]. If there was no element 1 in the content list (as when the element is empty), this will set $date to undef and might produce a warning.

Alternately, your use of content was very close: the content method returns an arrayref or undef, so you want my $date = $d->content->[1] but note that that will crash if the element has no content, so you might want to use eval: my $date = eval { $d->content->[1] } which will set $date to undef (and set $@ to an error about attempting to dereference something that was not a reference) if the element has no content.

(All code obviously untested.)

Replies are listed 'Best First'.
Re^2: HTML::Element accessing "internal attributes" the proper way
by LanX (Saint) on Dec 25, 2020 at 06:55 UTC
    > his will set $date to undef and might produce a warning.

    this would surprise me

    DB<71> use warnings; $a=(1..2)[9] DB<72>

    > so you might want to use eval: my $date = eval { $d->content->[1] }

    Thats why the use is of ->content is discouraged and ->content_array_ref is offered

    $content_array_ref = $h->content_array_ref(); # never undef

    "This is like content (with all its caveats and deprecations) except that it is guaranteed to return an array reference."

    > (All code obviously untested.)

    dito! :)

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

      I said "might" because I was not sure off the top of my head whether reading off the end of a list produces a warning in Perl or not.

      As the documentation says, content_array_ref is just as deprecated as content, with content_list being the preferred interface for new code.

        > As the documentation says, content_array_ref is just as deprecated as content

        Not really, I can't see this in the documentation.

        And the author seems to be a bit confused about the inner mechanics of Perl.

        update

        After further reading. .. the issue seems to be that the ref returned is the one of an internal attribute _content , which "still" allows direct write access to children elements, but that's strongly discouraged.

        But as long as you are only reading and not writing, I can't see a reason to deprecated the whole method.

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11125733]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-04-25 16:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found