LesleyB has asked for the wisdom of the Perl Monks concerning the following question:

Greetings Oh Great Ones

I have been studying perltoot and noticed something I'd like to be sure about.

Inherited Autoloaded Data Methods shows an Employee package inheriting from a Person package, where that package has autoloaded data methods.   Note carefully the Employee constructor does not bless $self into the Employee class.

At the end of Overridden Methods, just before Multiple Inheritance, the Employee constructor reconsecrates $self into the Employee class.

Having cut and pasted the code and constructed empty subclass tests, empirically at least, both constructors work.

I constructed the argument that the Employee class is sent into the Person class and therefore blessing in the Person package actually blesses the Person object into the Employee class, so the the $self returned from the inherited constructor is already in the Employee class. Therefore one might add to $self as demonstrated in the subclass constructor.

Which then left the dangling question on the purpose of the earlier example of reconsecration. I can only offer that it allows a subsequent class to be subclassed to the Employee class.

Are these two arguments reasonable?

Many thanks in advance for your replies

Regards

L.

Replies are listed 'Best First'.
Re: To bless or not to bless
by jettero (Monsignor) on Nov 30, 2009 at 14:42 UTC
    Autoloads are less popular without a good reason these days. They certainly have a purpose, but it should be fairly rare. If you want to avoid some of the work of blessing by hand, check out Moose — it's the new cool thing.

    I'm thinking I may have missed the question though, so perhaps I'm not 100% relevant. Still, if you haven't already: check out Moose. It's the new cool thing.

    -Paul

Re: To bless or not to bless
by Arunbear (Prior) on Nov 30, 2009 at 17:24 UTC
    The Employee constructor is:
    sub new { my $class = shift; my $self = $class->SUPER::new(); $self->{SALARY} = undef; $self->{ID} = undef; $self->{START_DATE} = undef; bless ($self, $class); # reconsecrate return $self; }
    When you do
    my $emp = Employee->new;
    the $self returned by
    my $self = $class->SUPER::new();
    will be blessed into the Person package, so to be able to call the methods that Employee has beyond those in Person, $self needs to be reblessed into the class that Employee::new was invoked on (i.e. Employee or a subclass of it).

    Update

    The answer above would apply if the first version of Person::new() in perltoot (i.e. the one that doesn't bless $self into $class) were being used. As ikegami points out, the rebless isn't needed if Person::new() does bless $self into $class.

      the $self returned by my $self = $class->SUPER::new(); will be blessed into the Person package,

      No it won't unless there's a bug in the parent class. $class contains "Employee", not "Person". The reblessing is unneeded.

        I've checked .....

        Given the following code

        I get the result

        Don Federico Jesus Pichon Alvarez is age 47. His peers are: PEON=FRANK, PEON=FELIPE, PEON=FAUST here is the boss $VAR1 = bless( { 'ID' => undef, 'PEERS' => [ 'Frank', 'Felipe', 'Faust' ], 'NAME' => undef, 'AGE' => 47, 'SALARY' => 40000, '_CENSUS' => \1, 'FULLNAME' => bless( { 'SURNAME' => 'Pichon Alvarez', 'TITLE' => 'Don', 'CHRISTIAN' => 'Federico Jesus +', 'NICK' => 'Fred' }, 'Fullname' ) }, 'Boss1' ); Destroying $self Fred at ./testboss1.pl line 0 All persons are going away now

        Boss1 inherits Employee4 which in turn inherits Person5. Neither Boss1 nor Employee4 bless. When a new Boss1 is instantiated the Employee4 constructor is called which in turn calls the Person5 constructor. The value of $class in both Person5 and Employee4 is Boss1. So Person5 artifacts (methods, globals and data) are blessed into the Boss1 class as are Employee4 artifacts.

        This would indicate that reblessing isn't entirely necessary, even when subclassing.

        But it might be reckless to assume that blessing occurs somewhere in the hierarchy.

      Thank you