Is there some standard on how to name variables in nested modules? Again and again, I am unsure whether I should name my variables this way or the other way round.
One style is that the local variable of a module is called $self or $this. Seems to be the common style.
So far so good. A problem arises when I use the local-module-var of the next level up within a nested module one level below. How should I name the former $self here? $toplevel_self? If I go down this path, I end up with some modules where I refer to a specific var by $self (probably the top-level module) and in other parts of my app, I name this var $that_toplevel_self, $parent or similar. This is especially awkward when the top-level $self contains some global config vars for my app.
So, is there a good argument for always naming my var $self/$this? Or is the other style I exemplify below equally sane, or even more helpful in a nested modules environment? Any hints which style I should use to adhere to "good coding style", or readability, when other developers join my projects?
Some pseudo-code to explain what I am talking about:
package App;
sub new {
my ($class, %args) = @_;
my $self = bless { %args }, $class;
$self->{widget} = App::Widget->new( $self );
return $self;
}
package App::Widget;
my( $class, $self ) = @_;
my $widget = bless { }, $class;
$self->{menu} = App::Widget::Menu->new( $self, $widget );
return $self;
}
package App::Widget::Menu;
sub new {
my( $class, $self, $widget ) = @_;
my $menu = bless { }, $class;
# let's do something with %{ $self } here;
$self->{some_arg} = 1;
$self->{menu}->BackgroundColor(222,222,222);
}
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.