=cut
sub parent {
my $self = shift;
if (@_) { # set
Carp::croak "an element can't be made its own parent"
if defined $_[0] and ref $_[0] and $self eq $_[0]; # sanity
$self->{'_parent'} = $_[0];
}
else {
$self->{'_parent'}; # get
}
}
=head2 $h->content_list()
Returns a list of the child nodes of this element -- i.e., what
nodes (elements or text segments) are inside/under this element. (Note
that this may be an empty list.)
In a scalar context, this returns the count of the items,
as you may expect.
=cut
sub content_list {
return
wantarray ? @{shift->{'_content'} || return()}
: scalar @{shift->{'_content'} || return 0};
}
=head2 $h->content()
This somewhat deprecated method returns the content of this element;
but unlike content_list, this returns either undef (which you should
understand to mean no content), or a I<reference to the array> of
content items, each of which is either a text segment (a string, i.e.,
a defined non-reference scalar value), or an HTML::Element object.
Note that even if an arrayref is returned, it may be a reference to an
empty array.
While older code should feel free to continue to use C<< $h->content >>,
new code should use C<< $h->content_list >> in almost all conceivable
cases. It is my experience that in most cases this leads to simpler
code anyway, since it means one can say:
@children = $h->content_list;
instead of the inelegant:
@children = @{$h->content || []};
If you do use C<< $h->content >> (or C<< $h->content_array_ref >>), you should not
use the reference returned by it (assuming it returned a reference,
and not undef) to directly set or change the content of an element or
text segment! Instead use L<content_refs_list> or any of the other
methods under "Structure-Modifying Methods", below.
=cut
# a read-only method! can't say $h->content( [] )!
sub content {
return shift->{'_content'};
}
=head2 $h->content_array_ref()
This is like C<content> (with all its caveats and deprecations) except
that it is guaranteed to return an array reference. That is, if the
given node has no C<_content> attribute, the C<content> method would
return that undef, but C<content_array_ref> would set the given node's
C<_content> value to C<[]> (a reference to a new, empty array), and
return that.
=cut
sub content_array_ref {
return shift->{'_content'} ||= [];
}
=head2 $h->content_refs_list
This returns a list of scalar references to each element of C<$h>'s
content list. This is useful in case you want to in-place edit any
large text segments without having to get a copy of the current value
of that segment value, modify that copy, then use the
C<splice_content> to replace the old with the new. Instead, here you
can in-place edit:
foreach my $item_r ($h->content_refs_list) {
next if ref $$item_r;
$$item_r =~ s/honour/honor/g;
}
You I<could> currently achieve the same affect with:
=5= |