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
oop perl


source to share


No one has answered this question yet

Check out similar questions:

1085
Naming classes - how to avoid calling the whole "<WhatEver> Manager"?
708
Interface vs Base class
633
What methods can be used to define a class in JavaScript and what are their tradeoffs?
594
How do I get the class of a JavaScript object?
551
Can we instantiate an abstract class?
541
How to fix warning about setting locale in Perl?
519
How to call a parent class function from a derived class function?
194
prototype based and class based inheritance
2
Java inheritance: Difference between IS-A and HAS-A relationships
1
Perl class attribute inheritance



All Articles
Loading...
X
Show
Funny
Dev
Pics