# man > Algorithm::C3(3pm)

---
type: CommandReference
command: Algorithm::C3
mode: man
section: 3pm
source: perldoc
---

## Quick Reference
- `use Algorithm::C3; my @merged = merge('D', \&get_supers);` — merge a diamond hierarchy
- `my @merged = merge('D', 'supers');` — use a method name to fetch parents
- `my %cache; my @merged = merge('D', \&get_supers, \%cache);` — merge with a shared cache

## Name
A module for merging hierarchies using the C3 algorithm

## Synopsis
perl
use Algorithm::C3;

# Diamond inheritance: D -> (B, C), B -> A, C -> A
my @merged = Algorithm::C3::merge('D', sub {
    no strict 'refs';
    @{$_[0] . '::ISA'};
});
print join ", " => @merged;  # prints D, B, C, A
## Functions
- **`merge($root, $func_to_fetch_parent, $cache)`** — computes the C3 linearization (MRO) starting from `$root`. Returns an ordered list of nodes.

  - **`$root`** — the starting node (e.g., a Perl package name)
  - **`$func_to_fetch_parent`** — a CODE ref or a string naming a method; called as `$func->($node)` to obtain the parent list
  - **`$cache`** (optional) — a hashref used as a private cache; reuse the same hash across multiple `merge` calls for better performance when parent results are stable

## Examples
perl
# Using a method name
{
    package A;
    sub supers { no strict 'refs'; @{$_[0] . '::ISA'}; }
    package B; our @ISA = ('A');
    package C; our @ISA = ('A');
    package D; our @ISA = ('B', 'C');
}
my @order = Algorithm::C3::merge('D', 'supers');
print join ", ", @order;  # D, B, C, A
perl
# Merging multiple hierarchies with a shared cache
sub do_some_merging {
    my %merge_cache;
    my @foo = Algorithm::C3::merge('Foo', \&get_supers, \%merge_cache);
    my @bar = Algorithm::C3::merge('Bar', \&get_supers, \%merge_cache);
}
## See Also
- [Class::C3](http://localhost/phpMan.php/perldoc/Class%3A%3AC3/markdown)
- [The Dylan Linearization (original C3 paper)](http://www.webcom.com/haahr/dylan/linearization-oopsla96.html)
- [Perl 6 MetaModel](http://svn.openfoundry.org/pugs/perl5/Perl6-MetaModel/)
- [Python 2.3 MRO](http://www.python.org/2.3/mro.html)
- [C3 for TinyCLOS](http://www.call-with-current-continuation.org/eggs/c3.html)