Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Advice please on variable naming style in modules

by Jenda (Abbot)
on Aug 21, 2012 at 21:41 UTC ( #988884=note: print w/ replies, xml ) Need Help??


in reply to Advice please on variable naming style in modules

Your variable naming is quite strange. $self should be the object whose method you are writing, not some parameter. That is within the App package it's the application, within App::Widget it's the widget and within App::Widget::Menu it's the menu. And you, generally, should not access the insides of the App from within an App::Widget::Menu method.

If I understand things right, you pass an app to widget constructor and both app and widget to menu. so the code should be

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, $app ) = @_; my $self = bless { }, $class; $app->{menu} = App::Widget::Menu->new( $app, $self); return $self; } package App::Widget::Menu; sub new { my( $class, $app, $widget ) = @_; my $self = bless { }, $class; $app->{some_arg} = 1; $app->{menu}->BackgroundColor(222,222,222); return $self; }

Jenda
Enoch was right!
Enjoy the last years of Rome.


Comment on Re: Advice please on variable naming style in modules
Download Code
Re^2: Advice please on variable naming style in modules
by isync (Hermit) on Aug 22, 2012 at 14:42 UTC
    Could be I was misleading you by a typo: App::Widget returns $widget, App::Widget::Menu returns $menu.

    The thing is, both App::Widget and App::Widget::Menu at times want to be able to access settings stored in App's $self. You can use a jump-the-hoops-method like my $self_of_the_topmost_level = wxTheApp->GetTopWindow(); (in a Wx app) to get that.

    Question is: what's a sane name for $self_of_the_topmost_level when it resides next to a "local" $self?

      What's "self" to you is isync to me. If the menu needs access to the top window, it should have a $top_window. The fact that within the methods of the top window the same reference will be in a variable called $self is irrelevant. To the method within the menu it's $top_window, not $self.

      <!-- Node text goes above. Div tags should contain sig only -- >

      Jenda
      Enoch was right!
      Enjoy the last years of Rome.

        Right, and more or less what tye and I agreed upon: what seems to make sense is: it's $self in the module and a var-name that reflects function everywhere else - as an example, for the $self of App, this could be $app (reflecting function), or could be $top_window (if you favour denoting hierarchical meaning).

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://988884]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2015-07-04 05:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (57 votes), past polls