Class::STL::Containers is a Perl modulethat provides a framework for rapid Object Oriented Perl application development. It consists of a number of base classes that are similar to the C++/STL framework, plus a number of helper classes which provide the glue to transparently generate common functions, and will enable you to put your Perl application together very quickly.
The STL functionality provided consists of containers, algorithms, utilities and iterators as follows:
Containers
vector, list, deque, queue, priority_queue, stack, tree.
Iterators
iterator, bidirectional_iterator, reverse_iterator, forward_iterator.
Algorithms
find, find_if, for_each, transform, count, count_if, copy, copy_backward, remove, remove_if, remove_copy, remove_copy_if, replace, replace_if, replace_copy, replace_copy_if.
Utilities
equal_to, not_equal_to, greater, greater_equal, less, less_equal, compare, bind1st, bind2nd, mem_fun, ptr_fun, ptr_fun_binary, matches, matches_ic, logical_and, logical_or, multiplies, divides, plus, minus, modulus.
SYNOPSIS
use stl;
# Deque container...
my $d = stl::deque(qw(first second third fourth));
$d->push_back($d->factory('fifth'));
$d->push_front($d->factory('seventh'));
$d->pop_front(); # remove element at front.
$d->pop_back(); # remove element at back.
stl::for_each($d->begin(), $d->end(), ptr_fun('::myprint'));
sub myprint { print "Data:", @_, "
"; }
# Copy constructor...
my $d_copy = stl::deque($d);
# Algorithms -- find_if()
print "Element 'second' was ",
stl::find_if($d->begin(), $d->end(), stl::bind1st(stl::equal_to(), 'second'))
? 'found' : 'not found', "
";
# Algorithms -- count_if()
print "Number of elements matching /o/ = ",
stl::count_if($d->begin(), $d->end(), stl::bind2nd(stl::matches(), 'o')),
"
"; # prints '2' -- matches 'second' and 'fourth'
# Algorithms -- transform()
stl::transform($d->begin(), $d->end(), $d2->begin(), stl::ptr_fun('ucfirst'));
stl::transform($d->begin(), $d->end(), $d2->begin(), $d3->begin(), stl::ptr_fun_binary('::mybfun'));
sub mybfun { return $_[0] . '-' . $_[1]; }
# Function Adaptors -- bind1st
stl::remove_if($v->begin(), $v->end(), stl::bind1st(stl::equal_to(), $v->back()));
# remove element equal to back() -- ie remove last element.
stl::remove_if($v->begin(), $v->end(), stl::bind2nd(stl::matches(), '^fi'));
# remove all elements that match reg-ex '^fi'
# Sort list according to elements cmp() function
$v->sort();
# Queue containers -- FIFO
my $v = stl::queue(qw(first second third fourth fifth));
print 'Back:', $v->back()->data(), "
" # Back:fifth
print 'Front:', $v->front()->data(), "
" # Front:first
$v->pop(); # pop element first in
$v->push($v->factory('sixth')), "
"
print 'Back:', $v->back()->data(), "
" # Back:sixth
print 'Front:', $v->front()->data(), "
" # Front:second
# Iterators
for (my $i = $v->begin(); !$i->at_end(); ++$i)
{
print "Data:", $i->p_element()->data();
}
# Iterators -- reverse_iterator
my $ri = stl::reverse_iterator($v->iter())->first();
while (!$ri->at_end())
{
print "Data:", $ri->p_element()->data();
++$ri;
}
# Inserters
my $three2one = stl::list(qw(3 2 1));
my $four2six = stl::list(qw(4 5 6));
my $seven2nine = stl::list(qw(7 8 9));
my $result = stl::list();
stl::copy($three2one->begin(), $three2one->end(), stl::front_inserter($result));
stl::copy($seven2nine->begin(), $seven2nine->end(), stl::back_inserter($result));
my $iseven = stl::find($result->begin(), $result->end(), 7);
stl::copy($four2six->begin(), $four2six->end(), stl::inserter($result, $iseven));
# $result now contains (1, 2, 3, 4, 5, 6, 7, 8, 9);
# Vector container...
my $v = stl::vector(qw(first second third fourth fifth));
my $e = $v->at(0); # return pointer to first element.
print 'Element-0:', $e->data(), "
"; # Element-0:first
$e = $v->at($v->size()-1); # return pointer to last element.
print 'Element-last:', $e->data(), "
"; # Element-last:fifth
$e = $v->at(2); # return pointer to 3rd element (idx=2).
print 'Element-2:', $e->data(), "
"; # Element-2:third
# Priority Queue
my $p = stl::priority_queue();
$p->push($p->factory(priority => 10, data => 'ten'));
$p->push($p->factory(priority => 2, data => 'two'));
$p->push($p->factory(priority => 12, data => 'twelve'));
$p->push($p->factory(priority => 3, data => 'three'));
$p->push($p->factory(priority => 11, data => 'eleven'));
$p->push($p->factory(priority => 1, data => 'one'));
$p->push($p->factory(priority => 1, data => 'one-2'));
$p->push($p->factory(priority => 12, data => 'twelve-2'));
$p->push($p->factory(priority => 20, data => 'twenty'), $p->factory(priority => 0, data => 'zero'));
print "$p->size()=", $p->size(), "
";
print "$p->top():", $p->top(), "
";
$p->top()->priority(7); # change priority for top element.
$p->refresh(); # refresh required after priority change.
$p->pop(); # remove element with highest priority.
print "$p->top():", $p->top(), "
";
# Clone $d container into $d1...
my $d1 = $d->clone();
my $d2 = stl::deque(qw(sixth seventh eight));
# Append $d container to end of $d2 container...
$d2 += $d;
# DataMembers -- Class builder helper...
{
package MyClass;
use Class::STL::ClassMembers (
qw(attrib1 attrib2), # data members
Class::STL::ClassMembers::DataMember->new(
name => 'attrib3', default => '100', validate => '^d+$'), # data member with attributes
Class::STL::ClassMembers::DataMember->new(
name => 'attrib4', default => 'med', validate => '^(high|med|low)$'),
);
use Class::STL::ClassMembers::Constructor; # produce class new() function
}
my $cl = MyClass->new(attrib1 => 'hello', attrib2 => 'world');
print $cl->attrib1(), " ", $cl->attrib2(), "
"; # 'hello world'
$cl->attrib1(ucfirst($cl->attrib1));
$cl->attrib2(ucfirst($cl->attrib2));
print $cl->attrib1(), " ", $cl->attrib2(), "
"; # 'Hello World'
$cl->attrib4('avg'); # Causes progam to die with '** Function attrib2 value failed validation...'
Product's homepage
Requirements:
· Perl