Calling base constructor in perl

前端 未结 3 607
走了就别回头了
走了就别回头了 2021-02-01 05:05

What is the correct way to call the base constructor from the class constructor in Perl ?

I have seen syntax like this:

 my $class = shift; 
 my $a = sh         


        
3条回答
  •  终归单人心
    2021-02-01 05:55

    If all your constructor is doing is calling the parent constructor (as in your example, you don't need to write one at all. Simply leave it out and the parent will be called; you just need to ensure that the object is blessed into the right type:

    package Parent;
    use strict;
    use warnings;
    
    sub new
    {
        my ($class, @args) = @_;
    
        # do something with @args
    
        return bless {}, $class;
    }
    1;
    

    If you use the above code and have a Child class declared with use parent 'Parent'; then the Parent constructor will properly construct a child.

    If you need to add some properties in the Child, then what you had is largely correct:

    package Child;
    use strict;
    use warnings;
    
    use parent 'Parent';
    
    sub new
    {
        my ($class, @args) = @_;
    
        # possibly call Parent->new(@args) first
        my $self = $class->SUPER::new(@args);
    
        # do something else with @args
    
        # no need to rebless $self, if the Parent already blessed properly
        return $self;
    }
    1;
    

    However, when you bring multiple inheritance into the mix, you need to decide the right thing to do at every step of the way. This means a custom constructor for every class that decides how to merge the properties of Parent1 and Parent2 into the child, and then finally blesses the resulting object into the Child class. This complication is one of many reasons why multiple inheritance is a bad design choice. Have you considered redesigning your object heirarchy, possibly by moving some properties into roles? Further, you might want to employ an object framework to take out some of the busy work, such as Moose. Nowadays it is rarely necessary to write a custom constructor.

    (Lastly, you should avoid using the variables $a and $b; they are treated differently in Perl as they are the variables used in sort functions and some other built-ins.)

提交回复
热议问题