Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Ovid (2709)

Ovid
  (email not shown publicly)
http://publius-ovidius.livejournal.com/
AOL IM: ovidperl (Add Buddy, Send Message)

Stuff with the Perl Foundation. A couple of patches in the Perl core. A few CPAN modules. That about sums it up.

Journal of Ovid (2709)

Saturday January 31, 2009
11:22 AM

Class::C3 Wierdness?

[ #38380 ]

Playing around with different MROs to try and figure out general purpose algorithms for detecting 'unreachable' methods. That's when I got confused by Class::C3. Imagine the following hierarchy. If every one of those classes has a 'target' method, I expect, with C3, that the calling order would be "One", "Two", "Three", "Four", "Six" and "Five".

     5
     |
     4  6
     | /
  2  3
   \ |
     1

Here's my code:

#!/usr/bin/perl -l

use strict;
use warnings;

package One;
our @ISA = qw/Two Three/;
use Class::C3;
sub target { print __PACKAGE__; shift->next::method }

package Two;
use Class::C3;
sub target { print __PACKAGE__; shift->next::method }

package Three;
our @ISA = qw/Four Six/;
use Class::C3;
sub target { print __PACKAGE__; shift->next::method }

package Four;
our @ISA = 'Five';
use Class::C3;
sub target { print __PACKAGE__; shift->next::method }

package Five;
use Class::C3;
sub target { print __PACKAGE__; shift->next::method }

package Six;
use Class::C3;
sub target { print __PACKAGE__; shift->next::method }

package main;
Class::C3::initialize();
One->target;

This prints out:

One
Two
Three
Four
Five
Six

Because 'Three' inherits from 'Four' and 'Six', I'm expecting 'Six' to get called before 'Five' (which 'Four' inherits from). What did I miss?

Note: the rationale behind this was simple. Let's say that only 'Two' and 'Six' define a 'target' method (the graphic I created to understand this). With Perl's default MRO, '&Six::target' is unreachable. I wasn't sure if that was the case with Class::C3. That's why I'm a bit confused.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • Hi,

    According to Class::C3 docs, the C3 algorithm only guarantees that no class Parent will be called before Child.

    In your case, when he gets to '4', he can call '5' because '6' is not a subclass of '5'.

    If you add Five to the @Six::ISA, then you would see the behavior that you are expecting.

    Best regards,

    --
    life is short