# NAME

Data::Container - Base class for objects containing a list of items

# SYNOPSIS

    package My::Container;
    use base 'Data::Container';
    # ...

    package main;
    my $container = My::Container->new;
    $container->items_push('some_item');

# DESCRIPTION

This class implements a container object. The container can contain any
object, scalar or reference you like. Typically you would subclass
Data::Container and implement custom methods for your specific container.

When the container is stringified, it returns the concatenated stringifications of its items, each two items joined by two newlines.

# METHODS

- `new`

    my $obj = Data::Container->new;
    my $obj = Data::Container->new(%args);

Creates and returns a new object. The constructor will accept as arguments a
list of pairs, from component name to initial value. For each pair, the named
component is initialized by calling the method of the same name with the given
value. If called with a single hash reference, it is dereferenced and its
key/value pairs are set as described before.

- `clear_items`

    $obj->clear_items;

Deletes all elements from the array.

- `count_items`

    my $count = $obj->count_items;

Returns the number of elements in the array.

- `index_items`

    my $element   = $obj->index_items(3);
    my @elements  = $obj->index_items(@indices);
    my $array_ref = $obj->index_items(@indices);

Takes a list of indices and returns the elements indicated by those indices.
If only one index is given, the corresponding array element is returned. If
several indices are given, the result is returned as an array in list context
or as an array reference in scalar context.

- `items`

    my @values    = $obj->items;
    my $array_ref = $obj->items;
    $obj->items(@values);
    $obj->items($array_ref);

Get or set the array values. If called without an arguments, it returns the
array in list context, or a reference to the array in scalar context. If
called with arguments, it expands array references found therein and sets the
values.

- `items_clear`

    $obj->items_clear;

Deletes all elements from the array.

- `items_count`

    my $count = $obj->items_count;

Returns the number of elements in the array.

- `items_index`

    my $element   = $obj->items_index(3);
    my @elements  = $obj->items_index(@indices);
    my $array_ref = $obj->items_index(@indices);

Takes a list of indices and returns the elements indicated by those indices.
If only one index is given, the corresponding array element is returned. If
several indices are given, the result is returned as an array in list context
or as an array reference in scalar context.

- `items_pop`

    my $value = $obj->items_pop;

Pops the last element off the array, returning it.

- `items_push`

    $obj->items_push(@values);

Pushes elements onto the end of the array.

- `items_set`

    $obj->items_set(1 => $x, 5 => $y);

Takes a list of index/value pairs and for each pair it sets the array element
at the indicated index to the indicated value. Returns the number of elements
that have been set.

- `items_shift`

    my $value = $obj->items_shift;

Shifts the first element off the array, returning it.

- `items_splice`

    $obj->items_splice(2, 1, $x, $y);
    $obj->items_splice(-1);
    $obj->items_splice(0, -1);

Takes three arguments: An offset, a length and a list.

Removes the elements designated by the offset and the length from the array,
and replaces them with the elements of the list, if any. In list context,
returns the elements removed from the array. In scalar context, returns the
last element removed, or `undef` if no elements are removed. The array grows
or shrinks as necessary. If the offset is negative then it starts that far
from the end of the array. If the length is omitted, removes everything from
the offset onward. If the length is negative, removes the elements from the
offset onward except for -length elements at the end of the array. If both the
offset and the length are omitted, removes everything. If the offset is past
the end of the array, it issues a warning, and splices at the end of the
array.

- `items_unshift`

    $obj->items_unshift(@values);

Unshifts elements onto the beginning of the array.

- `pop_items`

    my $value = $obj->pop_items;

Pops the last element off the array, returning it.

- `push_items`

    $obj->push_items(@values);

Pushes elements onto the end of the array.

- `set_items`

    $obj->set_items(1 => $x, 5 => $y);

Takes a list of index/value pairs and for each pair it sets the array element
at the indicated index to the indicated value. Returns the number of elements
that have been set.

- `shift_items`

    my $value = $obj->shift_items;

Shifts the first element off the array, returning it.

- `splice_items`

    $obj->splice_items(2, 1, $x, $y);
    $obj->splice_items(-1);
    $obj->splice_items(0, -1);

Takes three arguments: An offset, a length and a list.

Removes the elements designated by the offset and the length from the array,
and replaces them with the elements of the list, if any. In list context,
returns the elements removed from the array. In scalar context, returns the
last element removed, or `undef` if no elements are removed. The array grows
or shrinks as necessary. If the offset is negative then it starts that far
from the end of the array. If the length is omitted, removes everything from
the offset onward. If the length is negative, removes the elements from the
offset onward except for -length elements at the end of the array. If both the
offset and the length are omitted, removes everything. If the offset is past
the end of the array, it issues a warning, and splices at the end of the
array.

- `unshift_items`

    $obj->unshift_items(@values);

Unshifts elements onto the beginning of the array.

- `items_set_push`

Like `items_push()`, and it also takes a list of items to push into the
container, but it doesn't push any items that are already in the container
(items are compared deeply to determine equality).

- `item_grep`

Given a package name, it returns those items from the container whose `ref()`
is equal to that package name.

For example, your container could contain some items of type `My::Item::Foo`
and some of type `My::Item::Bar`. If you only want a list of the items of
type `My::Item::Foo`, you could use:

    my @foo_items = $container->item_grep('My::Item::Foo');

- `stringify`

Stringifies the data container by concatenating the items together, separated
by an empty line.

- `prepare_comparable`

Adds support for [Data::Comparable](http://search.cpan.org/perldoc?Data::Comparable) by autovivifying the container items
array.

# BUGS AND LIMITATIONS

No bugs have been reported.

Please report any bugs or feature requests through the web interface at
<http://rt.cpan.org>.

# INSTALLATION

See perlmodinstall for information and options on installing Perl modules.

# AVAILABILITY

The latest version of this module is available from the Comprehensive Perl
Archive Network (CPAN). Visit <http://www.perl.com/CPAN/> to find a CPAN
site near you. Or see <http://search.cpan.org/dist/Data-Container/>.

# AUTHORS

Marcel Gr&uuml;nauer, `<marcel@cpan.org>`

# COPYRIGHT AND LICENSE

Copyright 2004-2009 by the authors.

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.