I've recently been bitten by not being consistent. Before settling on one approach I'd appreciate the views of the monks.
For example, a new method calls a private init method. Should the init method return attributes?
sub new {
my ($class, $args) = @_;
my $self = {};
bless ($self, $class);
my ($attr_1, $attr_2) = $self->my_init($args);
$self->{attr_1} = $attr_1;
$self->{attr_2} = $attr_2;
return $self;
}
sub my_init {
my ($self, $args) = @_;
# do stuff with $args;
my $attr_1 = 1;
my $attr_2 = 2;
return ($attr_1, $attr_2);
}
or just set them:
sub new {
my ($class, $args) = @_;
my $self = {args => $args};
bless ($self, $class);
$self->my_init;
return $self;
}
sub my_init {
my ($self) = @_;
my $args = $self->{args}
# do stuff with $args
$self->{attr_1} = $attr_1;
$self->{attr_2} = $attr_2;
}
To my eye the latter looks neater but does the "action at a distance" make it less clear/maintainable than the former?
I'm using both approaches at the minute and I've met myself coming back. Are there better alternatives?
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.