Cold

Matts on 2001-11-09T17:19:21

It's approaching the middle of November, and here in Gloucester it's started to get really quite cold.

But, you know, I'm not bitter by any stretch. It's a million times warmer than it was when I was living in Scotland (really a million - try living there if you don't believe me). I think today will be the last day of "just a t-shirt" for a while though. It was just too cold. I would have only gotten away with a t-shirt up until september in scotland though.

Appologies to all scots. I'm sure some parts of your country are lovely (well, some parts are), but your weather sucks, and Livingston was just nasty.

Oh, and perl's OO is broken...

picture this: package Base;

my %FEATURES = (a => 1, b => 0, c => 1);

sub features { my @features; foreach (keys %FEATURES) { push @features, $_ if $FEATURES{$_}; } return @features; } 1;


and a sub-class: package SubClass; use base 'Base'; my %FEATURES = (a => 0, b => 1, c => 1);

1;


Now query SubClass->features(), and you get ('a', 'c').

This is covered somewhere in Damian's book, I recall, but it's a gotcha that people used to true OO should watch out for.

The way to fix it is to make the FEATURES hash into a method call that returns a hash. This way, when you call the base features() method, and it looks up the FEATURES hash, it gets the inherited method, and thus the right set of FEATURES.

I'm sure this will be fixed in Perl 6, otherwise I'll be around to kick Damian's butt ;-)


Not Broken

pudge on 2001-11-09T18:00:39

The idea of using a lexical variable for class data is what is broken. Your fix is a fine one ... but I see nothing to fix. Maybe there's a missing feature related to having simple declaration of class data, but there's no broken feature there.

Re:Not Broken

pudge on 2001-11-09T18:05:53

Just to further show how this is not anything "broken" in Perl, consider:

package main;
my %FEATURES = (a => 1, b => 0, c => 1);
print Base->features;

package Base;

sub features {
    my @features;
    foreach (keys %FEATURES) {
        push @features, $_ if $FEATURES{$_};
    }
    return @features;
}
1;

It returns ('a', 'c'), even though the variable is declared in main ... because the data is not declared in any package at all, really. Also, classes can be split between several files and scopes, and the lexical data in one scope will not be available in any other.

Re:Not Broken

Matts on 2001-11-10T16:03:42

Fine, it's a missing feature then.

It's still annoying, and something that would "work" in other OO languages, without using the data-as-sub work around.

Warm

vek on 2001-11-09T18:10:49

I'm originally from Cambridge (also pretty bloody cold at this time of the year) but have been living in the states for the past 6 years (the ol' wifey is American you see). So the deserts of Arizona are what I now call home. So, needless to say it's "just a t-shirt" for the entire year. Well, except for the sucky 5 months in the summer when it's 100-120 degrees outside, then its "just an air conditioner". I'm not going outside, nope you can't make me :) Oh, if you've ever wondered if you really can fry an egg on the path (s/path/sidewalk/g you americans you...) - you can - I dun it :)

Try Class::Data::Inheritable

autarch on 2001-11-10T03:18:08

I agree with pudge that this isn't really broken, but that's only cause you used 'my' instead of 'our' or a global (or something that would imply more of a 'class' scoping).

Perl does not have any way to inherit class data, which is lame.

Check out Schwern's Class::Data::Inheritable for a solution.

-dave

Re:Try Class::Data::Inheritable

Matts on 2001-11-10T16:01:22

It would have *exactly* the same results if I'd used "our".

Re:Try Class::Data::Inheritable

autarch on 2001-11-10T17:11:16

I know that!

My point was that you shouldn't even _expect_ it to work with 'my'. With 'our' (or a global) you might think that it _should_ work (though it wouldn't).

I was basically saying that even though pudge was right, he was missing your main point, which was simply that Perl should have inheritable class attributes.

-dave

Re:Try Class::Data::Inheritable

pudge on 2001-11-12T17:01:28

I wasn't missing that point; in fact, I specifically enunciated it. :-)