| Protocol-Yadis documentation | Contained in the Protocol-Yadis distribution. |
Protocol::Yadis::Document::Service::Element - Protocol::Yadis::Document::Service element object
my $e = Protocol::Yadis::Document::Service::Element->new;
$e->name('Type');
$e->attrs([a => 'b', c => 'd']);
$e->content('foo');
# <Type a="b" c="d">foo</Type>
This is an element object for Protocol::Yadis::Document::Service.
nameElement name.
contentElement content.
newCreates a new Protocol::Yadis::Document::Service::Element instance.
attrsSets/gets element attributes.
attrSets/gets element attribute.
to_stringString representation.
Viacheslav Tykhanovskyi, vti@cpan.org.
Copyright (C) 2009, Viacheslav Tykhanovskyi.
This program is free software, you can redistribute it and/or modify it under the same terms as Perl 5.10.
| Protocol-Yadis documentation | Contained in the Protocol-Yadis distribution. |
package Protocol::Yadis::Document::Service::Element; use strict; use warnings; use overload '""' => sub { shift->to_string }, fallback => 1; sub new { my $class = shift; my $self = {@_}; bless $self, $class; $self->{name} ||= ''; $self->{content} ||= ''; $self->{attrs} ||= []; return $self; } sub name { defined $_[1] ? $_[0]->{name} = $_[1] : $_[0]->{name} } sub content { defined $_[1] ? $_[0]->{content} = $_[1] : $_[0]->{content} } sub attrs { defined $_[1] ? $_[0]->{attrs} = $_[1] : $_[0]->{attrs} } sub attr { my $self = shift; my $name = shift; my $value = shift; my $attrs = $self->attrs; my $i = 0; for (; $i < @$attrs; $i += 2) { last if $attrs->[$i] eq $name; } if ($value) { if ($i >= @$attrs) { push @$attrs, ($name => $value); } else { $attrs->[$i + 1] = $value; } return $self; } return if $i >= @$attrs; return $attrs->[$i + 1]; } sub to_string { my $self = shift; my $name = $self->name; return unless $name; my $attrs = ''; for (my $i = 0; $i < @{$self->attrs}; $i += 2) { $attrs .= ' '; $attrs .= $self->attrs->[$i] . '="' . $self->attrs->[$i + 1] . '"'; } my $content = $self->content; return "<$name$attrs>$content</$name>"; } 1; __END__