Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Passing self from class to class?

by Anonymous Monk
on Aug 28, 2002 at 22:36 UTC ( #193602=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello fellow monks!

If you have time please take a look at the following code and answer the commented question.

Thanks!

eval { my $foo = new SomeClass; $SIG{__WARN__} = sub { die @_ }; $foo->initialize; $foo->run; }; if ($@) { print "content-type: text/html\n\n$@"; exit; } package SomeClass; sub new { bless { }, shift; } sub initialize { my $self = shift; require CGI; $self->{cgi} = new CGI; return $self; } sub run { my $self = shift; SomeOtherClass::SomeSub($self); # Is there any other way of sendin +g $self to SomeOtherClass without doing it this way? Note: I know I c +ould use @IAS and make SomeOtherClass a SomeClass but I'd rather not +create a new instance of the cgi module if it can't be helped. } package SomeOtherClass; sub SomeSub { my $self = shift; print $self->{cgi}->header, 'Hello World!'; }

Comment on Passing self from class to class?
Download Code
Re: Passing self from class to class?
by adrianh (Chancellor) on Aug 28, 2002 at 22:49 UTC

    I think you might actually be asking the wrong question..... however...

    If you really want to do this one way would be to re-bless the object into SomeOtherClass before the call. However this is almost certainly evil unless you're building some kind of state-transition pattern.

    More likely is that you want to setup some kind of has-a relation with a SomeOtherClass object and pass the object as an argument.

    Can you give more information on what you want SomeClass and SomeOtherClass to do? What is the relationship between the classes (and CGI)? Why don't you have SomeSub as a method of SomeClass?

    I suspect that once you've clarified this some kind of has-a of is-a relationship between SomeClass and SomeOtherClass will become obvious...

Re: Passing self from class to class?
by dws (Chancellor) on Aug 28, 2002 at 22:57 UTC
    It looks very much like you intent SomeClass to be a sublcass of SomeOtherClass. Why does it look this way? Because you assume that both classes have $self->{cgi} hold an instance of CGI, you show it being set up in SomeClass, but not in SomeOtherClass.

    If you change SomeClass to read

    package SomeClass; use base qw(SomeOtherClass); ...
    then you can write
    sub run { my $self = shift; $self->SomeSub(); }
    Doing this does not create a new instance of the CGI module. Methods (subroutines) from both SomeClass and SomeOtherClass both have access to the same instance of CGI.

Re: Passing self from class to class?
by Ovid (Cardinal) on Aug 28, 2002 at 23:05 UTC

    Maybe I am misunderstanding, but I think you would be better off subclassing your original class and calling the parent constructor. If a subclass is innapropriate, then SomeOtherClass should instantiate its own SomeClass object, store it in $self and use accessors that way. With either strategy, your eval will use SomeOtherClass instead of SomeClass.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: Passing self from class to class?
by Anonymous Monk on Aug 28, 2002 at 23:06 UTC
    Maybe this'll explain a bit more, I hope: Actually this is part of a fairly large script that I've broken down into different packages to help keep it easy to maintain. (By the way - from package to package subs might have the same name.) Not only the CGI module is loaded in SomeClass::initialize -- a configuration hash ref along with a whole bunch of other stuff is shoved into $self. Therefore, my main goal lately has been to come up with a design that allows easy access to $self script-wide. Passing $self as shown above (SomeOtherClass::SomeSub($self);) or using @IAS and creating another instance of everything (my $letsgo = new SomeOtherClass;$letsgo->SomeSub;) just doesn't seem like the best solution to me.

      I'm sorry. I'm still not understanding what you're trying to achieve. Probably me being dim... it's been known to happen...

      Can you give concrete examples of SomeClass and SomeOtherClass? What is the relationship between them? What are you trying to do? How are they used?

      Just some real names would help :-)

      What is the reason for SomeSub being a method of SomeOtherClass rather than SomeClass?

      Without knowning more about what you're trying to achieve it's difficult to give more useful advice...

        Let me make this a lot simpler: Is there a way to let multiple packages (with subs named the same) share one $self? My below example shows how I'd like to use this whole mess (see comments in the code for further details):
        eval { my $foo = OurParentPackage->new; $SIG{__WARN__} = sub { die @_ }; $foo->initialize; $foo->run; }; if ($@) { print "content-type: text/html\n\n$@"; exit; } package OurParentPackage; sub new { bless { }, shift; } sub initialize { my $self = shift; require CGI; $self->{CGI} = new CGI; $self->{Config} = { 'this' => 'that' }; # config hash would be loa +ded here. # and some 'global' stuff.... $self->{script_build} = 1; $self->{script_version} = '1.0 alpha'; return $self; } sub run { my $self = shift; if ($self->{CGI}->param('area') eq 'widgetone') { if ($self->{CGI}->param('act') eq 'add') { WidgetOne->add; } else { WidgetOne->default; } } elsif ($self->{CGI}->param('area') eq 'widgettwo') { if ($self->{CGI}->param('act') eq 'add') { WidgetTwo->add; } else { WidgetTwo->default; } } elsif ($self->{CGI}->param('area') eq 'widgetthree') { # etc.... } } package WidgetOne; sub default { my $self = shift; ############ # the below line wont work because it needs the the above subs $se +lf. I need to get its $self here somehow without re-running OurParent +Package's initialize sub. print $self->{CGI}->header; ############ } sub add { my $self = shift; # code to add a 'WidgetOne' would go here. } package WidgetTwo; sub default { my $self = shift; # the default 'WidgetTwo' page code would be here... } sub add { my $self = shift; # code to add a 'WidgetTwo' would go here. } package WidgetThree; sub default { my $self = shift; # the default 'WidgetThree' page code would be here... } sub add { my $self = shift; # code to add a 'WidgetTwo' would go here. }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (9)
As of 2014-12-23 03:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (135 votes), past polls