in reply to Re: Re: Package Variables in Inheritance
in thread Package Variables in Inheritance

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;

Replies are listed 'Best First'.
Re: Re: Re: Re: Package Variables in Inheritance
by tilly (Archbishop) on Nov 02, 2003 at 17:12 UTC
    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; }