Find the Bug!

Ovid on 2008-04-03T16:23:23

This was fun. Calls to $self->_initialize exposed a nasty bug. There's enough context below for you to see it.

sub _initialize {
    my $self = shift;
    $self->SUPER::_initialize;
    my $description = $self->{description};
    $self->{$_} = [] foreach $self->_can_contain;

    while ( my ( $type, $class ) = each %{ $self->_item_classes } ) {
        my $source = $self->source;
        foreach my $metadata ( @{ $description->{$type} } ) {
            push @{ $self->{$type} } =>
                $class->new_from_metadata( {
                    metadata => $metadata,
                    source   => $source,
                } );
        }
    }
}

sub _item_classes {
    my $self = shift;
    my %class_for = (
        attributes => 'Bermuda::Island::Attribute',
        elements   => 'Bermuda::Island::Element',
    );
    my %item_classes;
    foreach my $type ( $self->_can_contain ) {
        $item_classes{$type} = $class_for{$type};
    }
    return \%item_classes;
}

sub _can_contain { qw/ attributes elements / }


Nice... :)

mauzo on 2008-04-04T03:05:50

sub href {
    my %h = (
        foo => 1,
        bar => 2,
    );
    return \%h;
}

while (my ($k, $v) = %{ href() }) {
    print "$k => $v\n";
}

Bingo!

Ovid on 2008-04-04T06:18:01

Wow. People usually spot it sooner than this. Looks like I had a bit of stumper on this one. Good work :)