I seem to be on the other side of the fence on this one from everyone else. I think your first way seems a little cleaner, at the very least it's the way I tend to do things: inside my "init" I might call a "generate_something" method that returns "something" which I then use to set the "something" attribute.
What everyone else is telling you is essentially that the wall of the object class is the only encapsulation barrier that you need to respect, and when you're on the inside of it anything goes (or almost anything). What I would suggest is that the situation is a little fuzzier than that, and it's not a bad idea to try to organize the way communication happens inside of the object, as well as between objects.
Possibly a related issue: I often find that when I want to make sure that one thing is kept up to date if a change happens to something else, it's very tempting to bury a call inside the "something_else" accessor to some other routine that will refresh what ever needs to be kept up to date. That's a very robust (if not always efficient) way of doing things -- the trouble is when I come back six months later, I tend to forget about it, and assume that all my accessors are simple and stupid accessors. So: nice trick, but it's good to take it easy on it...
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.