For proper advice, some questions need answers. You label my_init() as a private method. Why? Is that method called from some other method (public or private) of the package? In your code examples my_init() is unconditionally called from new(). Why?
As you post them, your new() and my_init() methods could as well be merged into one. Make up your mind on why you need the attribute population separated from the constructor.
There are of course good reasons for doing that.
For either use, my preferred idiom is
sub new { my $class = shift; my $self = bless {}, $class; @_ ? $self->init(@_) : $self; } sub init { my $self = shift; # do parameter validation, set attributes ... # and return the object $self; }
IMHO attributes should only be passed around using setters/getters, and an init() subroutine is a catch-all setter method to me. It might do other initializing stuff, though.
--shmem
_($_=" "x(1<<5)."?\n".q·/)Oo. G°\ /
/\_¯/(q /
---------------------------- \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
In reply to Re: Passing object attributes around
by shmem
in thread Passing object attributes around
by wfsp
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |