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 ]

Matts (1087)

Matts
  (email not shown publicly)

I work for MessageLabs [messagelabs.com] in Toronto, ON, Canada. I write spam filters, MTA software, high performance network software, string matching algorithms, and other cool stuff mostly in Perl and C.

Journal of Matts (1087)

Friday November 09, 2001
12:19 PM

Cold

[ #1216 ]

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 ;-)

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.
  • 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.
    • 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,

    • 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.
  • 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 fr
  • 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