Algorithm-C3
view release on metacpan or search on metacpan
This takes a $root node, which can be anything really it is up to
you. Then it takes a $func_to_fetch_parent which can be either a
CODE reference (see SYNOPSIS above for an example), or a string
containing a method name to be called on all the items being
linearized. An example of how this might look is below:
{
package A;
sub supers {
no strict 'refs';
@{$_[0] . '::ISA'};
}
package C;
our @ISA = ('A');
package B;
our @ISA = ('A');
package D;
our @ISA = ('B', 'C');
}
print join ", " => Algorithm::C3::merge('D', 'supers');
The purpose of $func_to_fetch_parent is to provide a way for "merge"
to extract the parents of $root. This is needed for C3 to be able to
do it's work.
The $cache parameter is an entirely optional performance measure,
and should not change behavior.
If supplied, it should be a hashref that merge can use as a private
cache between runs to speed things up. Generally speaking, if you
will be calling merge many times on related things, and the parent
fetching function will return constant results given the same
arguments during all of these calls, you can and should reuse the
same shared cache hash for all of the calls. Example:
sub do_some_merging {
my %merge_cache;
my @foo_mro = Algorithm::C3::Merge('Foo', \&get_supers, \%merge_cache);
my @bar_mro = Algorithm::C3::Merge('Bar', \&get_supers, \%merge_cache);
my @baz_mro = Algorithm::C3::Merge('Baz', \&get_supers, \%merge_cache);
my @quux_mro = Algorithm::C3::Merge('Quux', \&get_supers, \%merge_cache);
# ...
}
CODE COVERAGE
I use Devel::Cover to test the code coverage of my tests, below is the
Devel::Cover report on this module's test suite.
------------------------ ------ ------ ------ ------ ------ ------ ------
File stmt bran cond sub pod time total
------------------------ ------ ------ ------ ------ ------ ------ ------
Algorithm/C3.pm 100.0 100.0 100.0 100.0 100.0 100.0 100.0
------------------------ ------ ------ ------ ------ ------ ------ ------
Total 100.0 100.0 100.0 100.0 100.0 100.0 100.0
------------------------ ------ ------ ------ ------ ------ ------ ------
SEE ALSO
The original Dylan paper
<http://www.webcom.com/haahr/dylan/linearization-oopsla96.html>
The prototype Perl 6 Object Model uses C3
<http://svn.openfoundry.org/pugs/perl5/Perl6-MetaModel/>
Parrot now uses C3
<http://aspn.activestate.com/ASPN/Mail/Message/perl6-internals/2746631>
<http://use.perl.org/~autrijus/journal/25768>
Python 2.3 MRO related links
<http://www.python.org/2.3/mro.html>
<http://www.python.org/2.2.2/descrintro.html#mro>
C3 for TinyCLOS
<http://www.call-with-current-continuation.org/eggs/c3.html>
AUTHORS
Stevan Little, <stevan@iinteractive.com>
Brandon L. Black, <blblack@gmail.com>
COPYRIGHT AND LICENSE
Copyright 2006 by Infinity Interactive, Inc.
<http://www.iinteractive.com>
This library is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
( run in 0.508 second using v1.01-cache-2.11-cpan-d7f47b0818f )