Javascript-Menu-Full
view release on metacpan or search on metacpan
Tree/Numbered.pm view on Meta::CPAN
# <new> constructs a new tree or node.
# Arguments: $value - the default value to be stored in the node.
# %extra - extra fields as name => value.
# Returns: The tree object.
sub new {
my ($parent, $value, $second, %extra) = @_;
my $class;
my $properties = {
Items => [],
Cursor => -1,
};
if ($class = ref($parent)) {
# Give it the same number as its parent.
$properties->{_LuckyNumber} = $parent->{_LuckyNumber};
$properties->{_ParentRef} = $parent;
bless $properties, $class;
# Inherit fields from parent (unless assigned in argument list).
$properties->addField($_, $parent->{$_})
foreach ($parent->getFieldNames);
} else {
$class = $parent;
$properties->{_LuckyNumber} = getNewLucky;
bless $properties, $class
}
$properties->{_Serial} = getNewSerial($properties->{_LuckyNumber});
# Detect parameter passing style for backward compatibility.
if (scalar @_ > 2) {
$extra{$value} = $second;
} elsif (scalar @_ == 2) {
$extra{Value} = $value if defined $value;
}
# Add requested fields, default to 'Value'.
foreach (keys %extra) {
# Calls are separated to override inherited settings.
$properties->addField($_);
$properties->setField($_, $extra{$_});
}
return $properties;
}
# <nextNode> moves the cursor forward by one.
# Arguments: None.
# Returns: Whatever is pointed by the cursor, undef on overflow, first item
# on subsequent overflow.
sub nextNode {
my $self = shift;
my $cursor = $self->{Cursor};
my $length = $self->childCount;
$cursor++;
# return undef when end of iterations. On next call - reset counter.
if ($cursor > $length) {
$cursor = ($length) ? 0 : -1;
}
$self->{Cursor} = $cursor;
if (exists $self->{Items}->[$cursor]) {
return $self->{Items}->[$cursor];
}
return undef;
}
# <reset> returns the counter to the beginning of the list.
# Arguments: None.
# Returns: Nothing.
sub reset {
my $self = shift;
$self->{Cursor} = -1;
}
# <delete> deletes the item pointed to by the cursor.
# The curser is not changed, which means it effectively moves to the next item.
# However it does change to be just after the end if it is already there,
# so you won't get an overflow.
# Arguments: None.
# Returns: The deleted item or undef if none was deleted.
sub delete {
my $self = shift;
my $cursor = $self->{Cursor};
if (exists $self->{Items}->[$cursor]) {
my $deleted = splice(@{$self->{Items}}, $cursor, 1);
# Make sure the cursor doesn't overflow:
if ($cursor > $self->childCount) {
$self->{Cursor} = $self->childCount;
}
return $deleted;
}
return undef;
}
# <append> adds a node at the end of the list.
# Arguments: parameters for new.
# Returns: The added node or undef on error.
sub append {
my $self = shift;
my $newNode = $self->new(@_);
return undef unless $newNode;
push @{$self->{Items}}, $newNode;
return $newNode;
}
# <savePlace> saves the place of the cursor.
# Arguments: None.
# Returns: Nothing.
( run in 1.299 second using v1.01-cache-2.11-cpan-71847e10f99 )