in reply to Re: OOP method usage
in thread OOP method usage

Thank you for your reply. That works like a charm. Am I able to use $self->_sub1; in a constructor? Or is it bad programming practice ? Example:
sub new { my $class = shift; my $self = { _txt => shift }; bless $self, $class; $self->_sub1(); return $self; }

Replies are listed 'Best First'.
Re^3: OOP method usage
by Athanasius (Archbishop) on Jul 07, 2012 at 09:56 UTC

    Yes, you can call an instance method on $self in the constructor, provided you have first called bless on it (as in your example).

    is it bad programming practice ?

    No, it’s standard practice. See perlobj:

    Once we've blessed the hash referred to by $self we can start calling methods on it. This is useful if you want to put object initialization in its own separate method:

    sub new { my $class = shift; my $self = {}; bless $self, $class; $self->_initialize(); return $self; }

    HTH, and welcome to the Monastery!

    Athanasius <°(((><contra mundum