How can I set a required attribute of my base class using class :: Std?

I am working with several different versions of Perl (all 5.8+) on different operating systems. I can't manage modules on different machines, which led me to use a combination of Class :: Std and a base pragma to facilitate OO Perl and inheritance.

I want to set a required attribute of my base class when creating a subclass object. Also, I want to do this without explicitly specifying the value in the arguments sent to my subclass, since it will be the same for all objects of that subclass.

The following code (followed by a question) illustrates this scenario:

My base class:

package Person;

use Class::Std;

# class data
my %person_title                :ATTR( name => "title"      );
my %person_name                 :ATTR( name => "name"       );
1;

      

My subclass:

package Doctor;

use Class::Std;

# set inheritance
use base 'Person';

# class data
my %doctor_specialty            :ATTR( name => "specialty"      );
1;

      

My script (currently this does not work with "Missing initializer label for Person: 'title'"):

use strict; 
use warnings;
use Doctor;

my %args = (
    "name"      => "John Smith",
    "specialty" => "Dentist",
);

Doctor->new(\%args);

      

When I create a Doctor object, I always want the "title" to be "Dr." How can I do that?

+3


source to share





All Articles