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)

Sunday February 01, 2009
09:51 AM

What Did I just Write?

[ #38384 ]

Drinking coffee. Hacking diligently. This code worked perfectly the first time I wrote it. I know what it's supposed to do, but I still can't believe it works. When my own code gives me a headache, I think I've made a mistake.

# This method builds 'paths'.  These are the paths the inheritance hierarchy
# will take through the code to find a method.  This is based on Perl's
# default search order, not C3.
sub _build_paths {
    my ( $self, $class, @parents ) = @_;

    my @paths = $self->paths;
    for my $i ( 0 .. $#paths ) {
        my $path = $paths[$i];
        if ( $path->[-1] eq $class ) {
            my @new_paths = map { [@$_] } ($path) x @parents;
            for my $j ( 0 .. $#parents ) {
                push @{ $new_paths[$j] } => $parents[$j];
            }
            splice @paths, $i, 1, @new_paths;
            $self->paths(@paths);
        }
    }
}

Yeah, let's try that again.

sub _build_paths {
    my ( $self, $class, @parents ) = @_;

    my @paths = $self->paths;
    for my $i ( 0 .. $#paths ) {
        my $path = $paths[$i];
        if ( $path->[-1] eq $class ) {
            my @new_paths;
            for my $parent_class (@parents) {
                push @new_paths => [@$path, $parent_class];
            }
            splice @paths, $i, 1, @new_paths;
            $self->paths(@paths);
        }
    }
}

Much better. I still have no idea why the first way popped into my damned head.

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.
  • First of all:

    sub _build_paths {
        my ( $self, $class, @parents ) = @_;

        my @paths = $self->paths;
        for my $i ( 0 .. $#paths ) {
            my $path = $paths[$i];
            if ( $path->[-1] eq $class ) {
                my @new_paths = map { [@$path, $_] } @parents;
                splice @paths, $i, 1, @new_paths;
                $self->paths(@paths);

  • Arg! You're right, I totally biffed that one. It took me long enough to realize the general shape of the algorithm that I just totally missed what I was iterating over. I've added your code.