in reply to Package Variables in Inheritance

Data you want to be inheirtable should go in the reference you are blessing (usually a hash). Use lexical variables (declared my at the top of your package) for private data.

----
I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
-- Schemer

: () { :|:& };:

Note: All code is untested, unless otherwise stated

Replies are listed 'Best First'.
Re: Re: Package Variables in Inheritance
by reyjrar (Hermit) on Oct 31, 2003 at 21:11 UTC
    Yes, I agree. This was more of a laziness thing than anything else. I have a lot of perl OO experience, just I had never attempted the package variable overloading before and was crossing my fingers that there'd be a way to do it. I understand why its not necessarily a good idea, but perhaps there are some cases where it could be useful (see my previous post). Maybe something like this:
    our $SheetName is overload;
    Maybe I'll look at doing something along those lines one day. I can't imagine it'd be too difficult. Or maybe something like C's extern, which says "this variable will be defined elsewhere" where elsewhere means a sub class of the current class. Uhm. I'm operating between Abstraction and Interfaces, and its confusing.

    Does anyone see any value in this?

    -brad..
      Use subs instead of data, so your example becomes:
      package Obj; use strict; sub name { undef } sub new { my $proto = shift; my $class = ref $proto || $proto; my $self = bless {}, $class; return $self; } sub test { my $self = shift; print $self->name, " \n"; } package Test; use strict; our @ISA=("Obj"); sub name { 'Testing' } package main; my $t = new Test; $t->test;
        Style notes on your new method.

        The ref $proto || $proto line is one which people disagree on the utility of. merlyn in particular is known for disliking it. My thoughts are milder, but I still dislike it. If you look in the thread starting here, I explain what it is supposed to do and why it doesn't really succeed.

        Also you are defining $self and then returning it. There is no need to do that. Just return the result of bless.

        And if you've made both of those changes, then there is no reason not to reduce a trivial new down to:

        sub new { bless {}, shift; }