sub all_attr_names {
return keys %{$_[0]};
}
=head2 $h->all_external_attr()
Like C<all_attr>, except that internal attributes are not present.
=head2 $h->all_external_attr_names()
Like C<all_external_attr_names>, except that internal attributes' names
are not present.
=cut
sub all_external_attr {
my $self = $_[0];
return
map(
(length($_) && substr($_,0,1) eq '_') ? () : ($_, $self->{$_}),
keys %$self
);
}
sub all_external_attr_names {
return
grep
!(length($_) && substr($_,0,1) eq '_'),
keys %{$_[0]}
;
}
=head2 $h->id() or $h->id($string)
Returns (optionally sets to C<$string>) the "id" attribute.
C<< $h->id(undef) >> deletes the "id" attribute.
=cut
sub id {
if(@_ == 1) {
return $_[0]{'id'};
} elsif(@_ == 2) {
if(defined $_[1]) {
return $_[0]{'id'} = $_[1];
} else {
return delete $_[0]{'id'};
}
} else {
Carp::croak '$node->id can\'t take ' . scalar(@_) . ' parameters!';
}
}
=head2 $h->idf() or $h->idf($string)
Just like the C<id> method, except that if you call C<< $h->idf() >> and
no "id" attribute is defined for this element, then it's set to a
likely-to-be-unique value, and returned. (The "f" is for "force".)
=cut
sub _gensym {
unless(defined $ID_COUNTER) {
# start it out...
$ID_COUNTER = sprintf('%04x', rand(0x1000));
$ID_COUNTER =~ tr<0-9a-f><J-NP-Z>; # yes, skip letter "oh"
$ID_COUNTER .= '00000';
}
++$ID_COUNTER;
}
sub idf {
my $nparms = scalar @_;
if ($nparms == 1) {
my $x;
if (defined($x = $_[0]{'id'}) and length $x) {
return $x;
}
else {
return $_[0]{'id'} = _gensym();
}
}
if ($nparms == 2) {
if (defined $_[1]) {
return $_[0]{'id'} = $_[1];
}
else {
return delete $_[0]{'id'};
}
}
Carp::croak '$node->idf can\'t take ' . scalar(@_) . ' parameters!';
}
=7= |